---
title: "Policy Representation Codebook for Zoli"
author: "G. Agustin Markarian, Revised by Mac Lockhart"
date: "July 22, 2020"
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
# List of required packages
required_packages <- c("readr", "haven", "tidyverse", "car", "ggplot2")

# Install any missing packages
installed_packages <- rownames(installed.packages())
for (pkg in required_packages) {
  if (!(pkg %in% installed_packages)) {
    install.packages(pkg, dependencies = TRUE)
  }
}

# Load the packages
lapply(required_packages, library, character.only = TRUE)

# Set root directory to the R Project's directory
# This ensures project portability across systems
directory.location <- rprojroot::find_root(rprojroot::is_rstudio_project)
knitr::opts_knit$set(root.dir = file.path(directory.location))

```

```{r }
paste0(directory.location, "/CCES Data/cces_2006_common.tab")
d06 <- read_delim(paste0(directory.location, "//CCES Data/cces_2006_common.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```


```{r}
#Subetting relevent colums
d06 <- subset(d06, v1000 != "977352") #broken row
d06 <- as.data.frame(select(d06, v2005, v2070, v2022, v2023, v2024))

#Renaming columns
colnames(d06) <- c("race",  "born_again", "religion_gen", "protestant_spec", "christian_spec")

d06 <- d06 %>%
  mutate(protestant_spec = tolower(protestant_spec),
         christian_spec = iconv(tolower(christian_spec), from = 'UTF-8', to = 'ASCII//TRANSLIT'),
         christian_spec = gsub("[[:punct:]]", "", christian_spec))


# Step 1: Create simplified denomination categories based on the Pew RELTRAD coding scheme
d06 <- d06 %>%
  mutate(
    protestant_clean = tolower(protestant_spec),
    christian_clean = tolower(christian_spec),

 # Evangelical denominations
    evangelical = case_when(
      str_detect(protestant_clean, "\\b(assembly of god|southern baptist|pentecostal|nazarene|foursquare|evangelical|free will baptist|church of god|assemblies of god|charismatic|wesleyan|holiness|nondenominational|non denominational)\\b") ~ 1,
      str_detect(christian_clean, "\\b(assembly of god|southern baptist|pentecostal|nazarene|foursquare|evangelical|free will baptist|church of god|assemblies of god|charismatic|wesleyan|holiness|nondenominational|non denominational)\\b") ~ 1,
      born_again == 1 ~ 1,  # Born-again indicator as evangelical
      TRUE ~ 0
    ),


    # Mainline denominations
    mainline = case_when(
      str_detect(protestant_clean, "\\b(episcopal|elca|lutheran|methodist|presbyterian|congregational|united church of christ|reformed|disciples of christ|pcusa|evangelical lutheran)\\b") ~ 1,
      str_detect(christian_clean, "\\b(episcopal|elca|lutheran|methodist|presbyterian|congregational|united church of christ|reformed|disciples of christ|pcusa|evangelical lutheran)\\b") ~ 1,
      TRUE ~ 0
    ),

    # Black Protestant denominations
    bprot = case_when(
      str_detect(protestant_clean, "\\b(ame|african methodist|cogic|missionary baptist|black baptist)\\b") | (str_detect(protestant_clean, "baptist")) ~ 1,
      str_detect(christian_clean, "\\b(ame|african methodist|cogic|missionary baptist|black baptist)\\b") | (str_detect(christian_clean, "baptist")) ~ 1,
      religion_gen == 1 & race==2 ~ 1,
      TRUE ~ 0
    ),

    # Catholic
    catholic = case_when(
      religion_gen == 2 ~ 1,
      TRUE ~ 0
    ),

    # Jewish
    jewish = case_when(
      religion_gen == 3 ~ 1,
      TRUE ~ 0
    ),

    # Other Religions
    other = case_when(
      religion_gen == 6 | religion_gen == 7 ~ 1,
      TRUE ~ 0
    ),

    # None
    none = case_when(
      religion_gen == 5 ~ 1,
      TRUE ~ 0
    )
  )

# Step 2: Create the final religion variable
d06_relig <- d06 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none, religion_gen) %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_
  ),

  # Fill remaining NAs based on most likely guess from religion_gen
  religion = case_when(
    is.na(religion) & religion_gen == 1 ~ "Mainline",  # Default for Protestants
    is.na(religion) & religion_gen == 2 ~ "Catholic",
    is.na(religion) & religion_gen == 3 ~ "Jewish",
    is.na(religion) & religion_gen == 4 ~ "Other",
    is.na(religion) & religion_gen == 5 ~ "None",
    is.na(religion) & religion_gen == 6 ~ "Other",
    is.na(religion) & religion_gen == 7 ~ "Mainline",
    TRUE ~ religion
  )) %>%
  select(religion)

# Display results
head(d06_relig)
```


```{r }
d06 <- read_delim(paste0(directory.location, "/CCES Data/cces_2006_common.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```

```{r}
#Subetting relevent colums
d06 <- subset(d06, v1000 != "977352") #broken row
d06 <- as.data.frame(select(d06, v1002,  v1003, v1004, v2004, v2005, v2018, v2020, v2021, v2032, v3007, v3004, v4062, vote_gen06, matched, v3060, v3063, v3066, v3069, v3072, v3075, v3078, v5022, v5024, v5026, v5028, v5030, v5032, v5034, v5023, v5025, v5027, v5029, v5031, v5033, v5035, v2089, v5047, v4063, v2026, v2027, v2029, v1001))

#Renaming columns
colnames(d06) <- c("state",  "cd", "fips", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "donate_any", "voter_ver", "vv_matched", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv1_s1", "rcv2_s1", "rcv3_s1", "rcv4_s1", "rcv5_s1", "rcv6_s1", "rcv7_s1", "rcv1_s2", "rcv2_s2", "rcv3_s2", "rcv4_s2", "rcv5_s2", "rcv6_s2", "rcv7_s2", "house_vote", "zip", "donate_amount", "church_attendance", "prayer", "importance", "weight")

d06$female <- car::recode(d06$female, "1=0;2=1")
d06$ideo <- ifelse(d06$ideo %in% c(6, 98, 99), 99, d06$ideo)
d06$income <- car::recode(d06$income, "c(1,2,3,4)=1; c(5,6,7,8,9)=2; c(10,11,12,13)=3; 14=4; c(15,98,99)=99")
d06$reg_voter <- car::recode(d06$reg_voter, "2=0;1=1;3=99;88=99")
d06$pid7 <- ifelse(d06$pid7>=8, 99, d06$pid7)
d06$donate_any <- car::recode(d06$donate_any, "1=1;2=0; c(3,98,99)=99")
d06$voter_ver <- car::recode(d06$voter_ver, "'2.0'=0;'1.0'=1;c('3.0','4.0','5.0','7.0','8.0')=9")
d06$year <- 2006
d06$vv_matched <- as.numeric(d06$vv_matched)
d06$house_vote <- car::recode(d06$house_vote,"1='Dem'; 2='Dem'; 3='Rep'; 4='Rep'; c(5,6,7)='Other'")
d06$pres_vote <- NA


rollcall_votes.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv1_s1", "rcv2_s1", "rcv3_s1", "rcv4_s1", "rcv5_s1", "rcv6_s1", "rcv7_s1", "rcv1_s2", "rcv2_s2", "rcv3_s2", "rcv4_s2", "rcv5_s2", "rcv6_s2", "rcv7_s2")

d06<-as.data.frame(d06)
for(i in 1:length(rollcall_votes.vec)){
  d06[ ,rollcall_votes.vec[i]] <- car::recode(d06[ ,rollcall_votes.vec[i]], "1=1;2=2;3=8;else=9")
}
d06 <- bind_cols(d06, d06_relig)
```


```{r }
d07 <- read_sav(paste0(directory.location, "/CCES Data/CCES07_OUTPUT.sav"), encoding="latin1")
```


```{r}
#Subetting relevent colums
d07 <- as.data.frame(select(d07, CC06_V2005, CC06_V2070, CC06_V2022, CC06_V2023, CC06_V2024))

#Renaming columns
colnames(d07) <- c("race",  "born_again", "religion_gen", "protestant_spec", "christian_spec")

d07 <- d07 %>%
  mutate(protestant_spec = tolower(protestant_spec),
         christian_spec = iconv(tolower(christian_spec), from = 'UTF-8', to = 'ASCII//TRANSLIT'),
         christian_spec = gsub("[[:punct:]]", "", christian_spec))


# Step 1: Create simplified denomination categories based on the Pew RELTRAD coding scheme
d07 <- d07 %>%
  mutate(
    protestant_clean = tolower(protestant_spec),
    christian_clean = tolower(christian_spec),

    # Evangelical denominations
    evangelical = case_when(
      str_detect(protestant_clean, "\\b(assembly of god|southern baptist|pentecostal|nazarene|foursquare|evangelical|free will baptist|church of god|assemblies of god|charismatic|wesleyan|holiness|nondenominational|non denominational)\\b") ~ 1,
      str_detect(christian_clean, "\\b(assembly of god|southern baptist|pentecostal|nazarene|foursquare|evangelical|free will baptist|church of god|assemblies of god|charismatic|wesleyan|holiness|nondenominational|non denominational)\\b") ~ 1,
      born_again == 1 ~ 1,  # Born-again indicator as evangelical
      TRUE ~ 0
    ),

    # Mainline denominations
    mainline = case_when(
      str_detect(protestant_clean, "\\b(episcopal|elca|lutheran|methodist|presbyterian|congregational|united church of christ|reformed|disciples of christ|pcusa|evangelical lutheran)\\b") ~ 1,
      str_detect(christian_clean, "\\b(episcopal|elca|lutheran|methodist|presbyterian|congregational|united church of christ|reformed|disciples of christ|pcusa|evangelical lutheran)\\b") ~ 1,
      TRUE ~ 0
    ),

    # Black Protestant denominations
    bprot = case_when(
      str_detect(protestant_clean, "\\b(ame|african methodist|cogic|missionary baptist|black baptist)\\b") | (str_detect(protestant_clean, "baptist")) ~ 1,
      str_detect(christian_clean, "\\b(ame|african methodist|cogic|missionary baptist|black baptist)\\b") | (str_detect(christian_clean, "baptist")) ~ 1,
      religion_gen == 1 & race==2 ~ 1,
      TRUE ~ 0
    ),

    # Catholic
    catholic = case_when(
      religion_gen == 2 ~ 1,
      TRUE ~ 0
    ),

    # Jewish
    jewish = case_when(
      religion_gen == 3 ~ 1,
      TRUE ~ 0
    ),

    # Other Religions
    other = case_when(
      religion_gen == 6 | religion_gen == 7 ~ 1,
      TRUE ~ 0
    ),

    # None
    none = case_when(
      religion_gen == 5 ~ 1,
      TRUE ~ 0
    )
  )

# Step 2: Create the final religion variable
d07_relig <- d07 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none, religion_gen) %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_
  ),

  # Fill remaining NAs based on most likely guess from religion_gen
  religion = case_when(
    is.na(religion) & religion_gen == 1 ~ "Mainline",  # Default for Protestants
    is.na(religion) & religion_gen == 2 ~ "Catholic",
    is.na(religion) & religion_gen == 3 ~ "Jewish",
    is.na(religion) & religion_gen == 4 ~ "Other",
    is.na(religion) & religion_gen == 5 ~ "None",
    is.na(religion) & religion_gen == 6 ~ "Other",
    is.na(religion) & religion_gen == 7 ~ "Mainline",
    TRUE ~ religion
  )) %>%
  select(religion)

# Display results
head(d07_relig)
```

```{r }
d07 <- read_sav(paste0(directory.location, "/CCES Data/CCES07_OUTPUT.sav"), encoding="latin1")
```

```{r}
#Subetting relevent colums
d07 <- as.data.frame(select(d07, CC06_V1002, CC06_V1003, CC06_V1004, CC06_V2004, CC06_V2005, CC06_V2018, CC06_V2020, CC06_V2021, CC06_V2032, CC06_V3007, CC06_V3004, CC06_V4062,  CC06_V3060, CC06_V3063, CC06_V3066, CC06_V3069, CC06_V3072, CC06_V3075, CC06_V3078, CC06_V5022, CC06_V5024, CC06_V5026, CC06_V5028, CC06_V5030, CC06_V5032, CC06_V5034, CC06_V5023, CC06_V5025, CC06_V5027, CC06_V5029, CC06_V5031, CC06_V5033, CC06_V5035, CC06_V4015, inputzip, CC06_V4063, CC06_V2026, CC06_V2027, CC06_V2029, weight))

#Renaming columns
colnames(d07) <- c("state",  "cd", "fips", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "donate_any",  "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv1_s1", "rcv2_s1", "rcv3_s1", "rcv4_s1", "rcv5_s1", "rcv6_s1", "rcv7_s1", "rcv1_s2", "rcv2_s2", "rcv3_s2", "rcv4_s2", "rcv5_s2", "rcv6_s2", "rcv7_s2", "house_vote", "zip", "donate_amount", "church_attendance", "prayer", "importance", "weight")

d07$female <- car::recode(d07$female, "1=0;2=1")
d07$ideo <- ifelse(d07$ideo==6, 99, d07$ideo)
d07$income <- car::recode(d07$income, "c(1,2,3,4)=1; c(5,6,7,8,9)=2; c(10,11,12,13)=3; 14=4; 15=99")
d07$reg_voter <- car::recode(d07$reg_voter, "2=0;1=1;c(3,8,9)=99")
d07$pid7 <- ifelse(d07$pid7 %in% c(8,9,98,99), 99, d07$pid7)
d07$voter_ver <- NA
d07$donate_any <- car::recode(as.numeric(d07$donate_any), "1=1;2=0; c(3,98,99)=99")
d07$year <- 2007
d07$house_vote <- car::recode(as.numeric(d07$house_vote),"1='Dem'; 2='Rep';c(3,4,5,6,7,8,98,99)='Other'")
d07$pres_vote <-NA
d07$zip <- as.character(d07$zip)

rollcall_votes.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv1_s1", "rcv2_s1", "rcv3_s1", "rcv4_s1", "rcv5_s1", "rcv6_s1", "rcv7_s1", "rcv1_s2", "rcv2_s2", "rcv3_s2", "rcv4_s2", "rcv5_s2", "rcv6_s2", "rcv7_s2")

for(i in 1:length(rollcall_votes.vec)){
  d07[ ,rollcall_votes.vec[i]] <- car::recode(d07[ ,rollcall_votes.vec[i]], "1=1;2=2;c(3,8)=8;else=9")
}
d07 <- bind_cols(d07, d07_relig)
```

```{r}
d08 <- read_delim(paste0(directory.location, "/CCES Data/cces_2008_common.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)

d08_s1 <- read_csv(paste0(directory.location, "/CCES Data/Senator+1+Roll+Call.csv"))

d08_s2 <- read_csv(paste0(directory.location, "/CCES Data/Senator+2+Roll+Call.csv"))

d08_h <- read_csv(paste0(directory.location, "/CCES Data/House+Roll+Call.csv"))
```

```{r}
d08_h <- d08_h[,c(3,4,6:14)]
colnames(d08_h) <- c("state", "cd", "hr_party", "rcv1_h", "rcv2_h", "rcv3_h", "rcv4_h", "rcv5_h", "rcv6_h", "rcv7_h" , "rcv8_h")
d08_s1 <- d08_s1[,c(1,4,6:11)]
colnames(d08_s1) <- c("state", "s1_party","rcv2_s1", "rcv3_s1", "rcv4_s1", "rcv5_s1", "rcv6_s1", "rcv7_s1" )
d08_s2 <- d08_s2[,c(1,4,6:11)] 
colnames(d08_s2)<- c("state", "s2_party",  "rcv2_s2", "rcv3_s2", "rcv4_s2", "rcv5_s2", "rcv6_s2", "rcv7_s2")
```

```{r}
d08 <- d08 %>% 
  mutate(pew_churatd = V217, religpew = V219, religpew_protestant= V220, religpew_baptist = V222, religpew_methodist = V223, religpew_nondenom = V224, religpew_lutheran = V225, religpew_presby = V226, religpew_pentecost = V227, religpew_episcop = V228, religpew_christian = V229, religpew_congreg = V230, religpew_holiness = V231, religpew_reformed = V232, religpew_advent = V233, religpew_catholic = V234, religpew_mormon = V235, religpew_orthodox = V236, religpew_jewish = V237, religpew_muslim = V238, religpew_buddhist = V239, religpew_hindu = V240)

d08 <- d08 %>% 
  mutate(race = V211)


d08 <- d08 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d08 <- d08 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d08 <- d08 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d08 <- d08 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d08 <- d08 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d08 <- d08 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d08 <- d08 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d08 <- d08 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d08 <- d08 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d08 <- d08 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d08 <- d08 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d08 <- d08 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d08 <- d08 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d08 <- d08 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d08 <- d08 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d08 <- d08 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d08 <- d08 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d08 <- d08 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d08 <- d08 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d08 <- d08 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d08 <- d08 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d08 <- d08 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d08 <- d08 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d08 <- d08 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d08 <- d08 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d08 <- d08 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d08 <- d08 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d08 <- d08 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d08 <- d08 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d08 <- d08 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d08 <- d08 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d08 <- d08 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d08 <- d08 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d08 <- d08 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d08 <- d08 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d08 <- d08 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d08 <- d08 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d08 <- d08 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d08 <- d08 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d08 <- d08 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d08 <- d08 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d08 <- d08 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d08 <- d08 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d08 <- d08 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

d08_relig <- select(d08, c(pew_churatd:none))

d08_relig <- d08_relig %>%
  rowwise() %>%
  mutate(tot = sum(c_across(religpew_baptist:none)))

# Finishing up
d08_relig <- d08_relig %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d08_relig$religion <- factor(d08_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d08 <- read_delim(paste0(directory.location, "/CCES Data/cces_2008_common.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```


```{r}
#Subetting releventolums
d08 <- as.data.frame(select(d08, V206, V250, V251 , V208, V211, V213, V207, V243, V246,  CC307a, V203, CC415_6, vote_gen08, voter_status, CC316a, CC316b, CC316c, CC316d, CC316e, CC316f, CC316g, CC316h, CC316i, CC339, CC326b, CC327, V202, CC416b, V215, V216, V217, V218, V201))

#Renaming columns
colnames(d08) <- c("state", "cd", "fips", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "donate_any", "voter_ver", "voter_status", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r", "house_vote", "pres_votea", "pres_voteb", "zip", "donate_amount", "born_again", "importance", "church_attendance", "prayer", "weight")

#Recode variables
d08$female <- car::recode(d08$female, "1=0;2=1")
d08$ideo <- ifelse(d08$ideo %in% c(6, 98, 99), 99, d08$ideo)
d08$income <- car::recode(d08$income, "c(1,2,3,4)=1; c(5,6,7,8,9)=2; c(10,11,12,13)=3; 14=4; c(15,98,99)=99")
d08$reg_voter <- car::recode(d08$reg_voter, "2=0;1=1;3=99")
d08$pid7 <- ifelse(d08$pid7==8, NA, d08$pid7)
d08$voter_ver <- car::recode(d08$voter_ver, "2=0;1=1;c(3,4,5,7,8)=99")
d08$donate_any <- car::recode(d08$donate_any, "1=1;2=0; c(3,98,99)=99")
d08$year <- 2008
d08$house_vote <- car::recode(d08$house_vote,"1='Dem'; 2='Rep';c(3,4,5,6,7,8,9,10,98,99)='Other'") 
d08$pres_votea <-car::recode(d08$pres_votea,"1='Rep'; 2='Dem';c(3,4,5,6,7,8,9)='Other'") 
d08$pres_voteb <-car::recode(d08$pres_voteb,"1='Rep'; 2='Dem';c(3,4,5,6,7,8,9)='Other'") 
d08$pres_vote <- ifelse(!is.na(d08$pres_votea), d08$pres_votea, d08$pres_voteb)
d08$pres_votea<-NULL 
d08$pres_voteb<-NULL

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r",  "rcv8_r","rcv9_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d08[ ,rollcall_votes_r.vec[i]] <- car::recode(d08[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;8=8;9=9;else=NA")
}

d08 <- bind_cols(d08, d08_relig)
```

```{r}
#Clearing for memory
d08_s1 <- NULL
d08_s2 <- NULL
d08_h <- NULL
d08_relig <- NULL
```

```{r}
d09 <-read_dta(paste0(directory.location, "/CCES Data/cces09_cmn_output_2.dta"), encoding = "latin1")
```

```{r}
d09 <- d09 %>% 
  mutate(pew_churatd = v217, religpew = v219, religpew_protestant= v220, religpew_baptist = v222, religpew_methodist = v223, religpew_nondenom = v224, religpew_lutheran = v225, religpew_presby = v226, religpew_pentecost = v227, religpew_episcop = v228, religpew_christian = v229, religpew_congreg = v230, religpew_holiness = v231, religpew_reformed = v232, religpew_advent = v233, religpew_catholic = v234, religpew_mormon = v235, religpew_orthodox = v236, religpew_jewish = v237, religpew_muslim = v238, religpew_buddhist = v239, religpew_hindu = v240)

d09 <- d09 %>% 
  mutate(race = v211)


d09 <- d09 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d09 <- d09 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d09 <- d09 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d09 <- d09 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d09 <- d09 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d09 <- d09 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d09 <- d09 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d09 <- d09 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d09 <- d09 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d09 <- d09 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d09 <- d09 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d09 <- d09 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d09 <- d09 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d09 <- d09 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d09 <- d09 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d09 <- d09 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d09 <- d09 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d09 <- d09 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d09 <- d09 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d09 <- d09 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d09 <- d09 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d09 <- d09 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d09 <- d09 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d09 <- d09 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d09 <- d09 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d09 <- d09 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d09 <- d09 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d09 <- d09 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d09 <- d09 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d09 <- d09 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d09 <- d09 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d09 <- d09 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d09 <- d09 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d09 <- d09 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d09 <- d09 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d09 <- d09 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d09 <- d09 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d09 <- d09 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d09 <- d09 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d09 <- d09 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d09 <- d09 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d09 <- d09 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d09 <- d09 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d09 <- d09 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d09_relig <- d09 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d09_relig$religion <- factor(d09_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d09 <-read_dta(paste0(directory.location, "/CCES Data/cces09_cmn_output_2.dta"), encoding = "latin1")
```


```{r}
#Subetting relevent colums
d09 <- as.data.frame(select(d09, v291, v264, v269, v208, v211, v213, v207, v261 , v246 , cc424, v256, cc09_59a , cc09_59b , cc09_59c , cc09_59d , cc09_59e, cc09_59f, cc09_59g, cc09_59h, cc09_32, cc09_31, v253, v215, v216, v217, v218, v200))

#Renaming columns
colnames(d09) <- c("state", "cd", "fips", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter",  "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "house_vote", "pres_vote", "zip", "born_again", "importance", "church_attendance", "prayer", "weight")

d09$female <- car::recode(d09$female, "1=0;2=1")
d09$ideo <- ifelse(d09$ideo==6, 99, d09$ideo)
d09$income <- car::recode(d09$income, "c(1,2,3,4)=1; c(5,6,7,8,9)=2; c(10,11,12,13)=3; 14=4; 15=99")
d09$reg_voter <- car::recode(d09$reg_voter, "2=0;1=1;c(3,8,9)=99")
d09$pid7 <- ifelse(d09$pid7 %in% c(8,9,98,99), 99, d09$pid7)
d09$voter_ver <- NA
d09$donate_any <- NA
d09$year <- 2009
d09$donate_any <- NA
d09$house_vote <- car::recode(as.numeric(d09$house_vote),"1='Dem'; 2='Rep';c(3,4,5,8,9)='Other'") 
d09$pres_vote <-car::recode(as.numeric(d09$pres_vote),"1='Dem'; 2='Rep';c(3,4,5,8,9)='Other'") 
d09$donate_amount <- NA


d09 <- bind_cols(d09, d09_relig)
```

```{r}
d10 <- read_dta(paste0(directory.location, "/CCES Data/cces_2010_common_validated.dta"))
```

```{r}
d10 <- d10 %>% 
  mutate(pew_churatd = V217, religpew = V219, religpew_protestant= V220, religpew_baptist = V222, religpew_methodist = V223, religpew_nondenom = V224, religpew_lutheran = V225, religpew_presby = V226, religpew_pentecost = V227, religpew_episcop = V228, religpew_christian = V229, religpew_congreg = V230, religpew_holiness = V231, religpew_reformed = V232, religpew_advent = V233, religpew_catholic = V234, religpew_mormon = V235, religpew_orthodox = V236, religpew_jewish = V237, religpew_muslim = V238, religpew_buddhist = V239, religpew_hindu = V240)

d10 <- d10 %>% 
  mutate(race = V211)

d10 <- d10 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d10 <- d10 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d10 <- d10 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d10 <- d10 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d10 <- d10 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d10 <- d10 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d10 <- d10 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d10 <- d10 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d10 <- d10 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d10 <- d10 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d10 <- d10 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d10 <- d10 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d10 <- d10 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d10 <- d10 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d10 <- d10 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d10 <- d10 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d10 <- d10 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d10 <- d10 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d10 <- d10 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d10 <- d10 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d10 <- d10 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d10 <- d10 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d10 <- d10 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d10 <- d10 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d10 <- d10 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d10 <- d10 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d10 <- d10 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d10 <- d10 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d10 <- d10 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d10 <- d10 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d10 <- d10 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d10 <- d10 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d10 <- d10 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d10 <- d10 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d10 <- d10 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d10 <- d10 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d10 <- d10 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d10 <- d10 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d10 <- d10 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d10 <- d10 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d10 <- d10 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d10 <- d10 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d10 <- d10 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d10 <- d10 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d10_relig <- d10 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d10_relig$religion <- factor(d10_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d10 <- read_dta(paste0(directory.location, "/CCES Data/cces_2010_common_validated.dta"))
```


```{r}
#Subetting releventolums
d10 <- as.data.frame(select(d10, V206, V276, V302, V208, V211, V213, V207, V243, V212d , V246, V203, CC417a_4, vote_gen10, voter_status, CC332A, CC332B, CC332C, CC332D, CC332E, CC332F, CC332G, CC332H, CC332I, CC332J, V507, V508, V509, V510, V511, V512, V516, V517, V518, V519, V524, V525, V526, V527, CC390, CC317, V201, CC417c, V215, V216, V217, V218, V101))

#Renaming columns
colnames(d10) <- c("state", "cd", "fips", "female", "race", "edu", "dob", "ideo",  "pid7", "income", "reg_voter", "donate_any", "voter_ver", "vv_status", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r", "rcv10_r", "rcv1_h", "rcv3_h", "rcv7_h" ,"rcv6_h","rcv4_h", "rcv2_h", "rcv1_s1"  ,"rcv6_s1","rcv4_s1", "rcv2_s1", "rcv1_s2"  ,"rcv6_s2","rcv4_s2", "rcv2_s2", "house_vote", "pres_vote", "zip", "donate_amount", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d10$female <- car::recode(d10$female, "1=0;2=1")
d10$ideo <- ifelse(d10$ideo %in% c(6, 98, 99), 99, d10$ideo)
d10$income <- car::recode(d10$income, "c(1,2,3,4)=1; c(5,6,7,8,9)=2; c(10,11,12,13)=3; 14=4; c(15,98,99)=99")
d10$reg_voter <- car::recode(d10$reg_voter, "2=0;1=1;3=99")
d10$pid7 <- ifelse(d10$pid7==8, NA, d10$pid7)
d10$voter_ver <- car::recode(d10$voter_ver, "2=0;1=1;c(3,4,5,7,8)=99")
d10$donate_any <- car::recode(d10$donate_any, "1=1;2=0; c(3,98,99)=99")
d10$year <- 2010
d10$house_vote <- car::recode(as.numeric(d10$house_vote),"1='Dem'; 2='Rep';c(3,4,7,8,9)='Other'") 
d10$pres_vote <-car::recode(as.numeric(d10$pres_vote),"1='Dem'; 2='Rep';c(3,4,5,8,9)='Other'") 

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r",  "rcv8_r", "rcv9_r" , "rcv10_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d10[ ,rollcall_votes_r.vec[i]] <- car::recode(d10[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}


rollcall_votes_cong.vec <-c("rcv1_h", "rcv3_h", "rcv7_h" ,"rcv6_h","rcv4_h", "rcv2_h", "rcv1_s1"  ,"rcv6_s1","rcv4_s1", "rcv2_s1", "rcv1_s2"  ,"rcv6_s2","rcv4_s2", "rcv2_s2")

for(i in 1:length(rollcall_votes_cong.vec)){
  d10[ ,rollcall_votes_cong.vec[i]] <- car::recode(d10[ ,rollcall_votes_cong.vec[i]], "'For'=1;'Against'=0;'Did Not Vote'=9;''=9")
}

d10 <- bind_cols(d10, d10_relig)
```

```{r}
d11 <-  read_delim(paste0(directory.location, "/CCES Data/CCES11_Common_OUTPUT.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```

```{r}
d11 <- d11 %>% 
  mutate(pew_churatd = V217, religpew = V219, religpew_protestant= V220, religpew_baptist = V222, religpew_methodist = V223, religpew_nondenom = V224, religpew_lutheran = V225, religpew_presby = V226, religpew_pentecost = V227, religpew_episcop = V228, religpew_christian = V229, religpew_congreg = V230, religpew_holiness = V231, religpew_reformed = V232, religpew_advent = V233, religpew_catholic = V234, religpew_mormon = V235, religpew_orthodox = V236, religpew_jewish = V237, religpew_muslim = V238, religpew_buddhist = V239, religpew_hindu = V240)


d11 <- d11 %>% 
  mutate(race = V211)

d11 <- d11 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))


d11 <- d11 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d11 <- d11 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d11 <- d11 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d11 <- d11 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d11 <- d11 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d11 <- d11 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d11 <- d11 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d11 <- d11 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d11 <- d11 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d11 <- d11 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d11 <- d11 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d11 <- d11 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d11 <- d11 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d11 <- d11 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d11 <- d11 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d11 <- d11 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d11 <- d11 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d11 <- d11 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d11 <- d11 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d11 <- d11 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d11 <- d11 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d11 <- d11 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d11 <- d11 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d11 <- d11 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d11 <- d11 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d11 <- d11 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d11 <- d11 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d11 <- d11 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d11 <- d11 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d11 <- d11 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d11 <- d11 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d11 <- d11 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d11 <- d11 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d11 <- d11 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d11 <- d11 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d11 <- d11 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d11 <- d11 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d11 <- d11 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d11 <- d11 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d11 <- d11 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d11 <- d11 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d11 <- d11 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d11 <- d11 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d11 <- d11 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d11_relig <- d11 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d11_relig$religion <- factor(d11_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d11 <-  read_delim(paste0(directory.location, "/CCES Data/CCES11_Common_OUTPUT.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```

```{r}
#Subetting releventolums
d11 <- as.data.frame(select(d11, V206, V276, V302, V208, V211, V213, V207, V243, V246, V212d , V203,  CC341A, CC341B, CC341C, CC341D, CC341E, CC341F, CC341G, CC341H, V521, V501, V513, CC333, CC331, V201, V215, V217, V218, V101))

#Renaming columns
colnames(d11) <- c("state", "cd", "fips", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "house_name", "sen1_name", "sen2_name", "house_vote", "pres_vote", "zip", "born_again", "church_attendance" , "prayer", "weight")
 
#Recode variables
d11$female <- car::recode(d11$female, "1=0;2=1")
d11$ideo <- ifelse(d11$ideo %in% c(6, 98, 99), 99, d11$ideo)
d11$income <- car::recode(d11$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16,31,18,32)=4; c(97,98,99)=99")
d11$reg_voter <- car::recode(d11$reg_voter, "2=0;1=1;3=99")
d11$pid7 <- ifelse(d11$pid7==8, NA, d11$pid7)
d11$year <- 2011
d11$house_vote <- car::recode(as.numeric(d11$house_vote),"1='Dem'; 2='Rep';c(3,4,5)='Other'") 
d11$pres_vote <-car::recode(as.numeric(d11$pres_vote),"1='Dem'; 2='Rep';c(3,4,5)='Other'") 
d11$donate_amount <- NA

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r",  "rcv8_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d11[ ,rollcall_votes_r.vec[i]] <- car::recode(d11[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d11 <- bind_cols(d11, d11_relig)
```

```{r}
d12 <- read_delim(paste0(directory.location, "/CCES Data/CCES12_Common_VV.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```

```{r}
d12 <- d12 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d12 <- d12 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d12 <- d12 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d12 <- d12 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d12 <- d12 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d12 <- d12 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d12 <- d12 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d12 <- d12 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d12 <- d12 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d12 <- d12 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d12 <- d12 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d12 <- d12 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d12 <- d12 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d12 <- d12 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d12 <- d12 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d12 <- d12 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d12 <- d12 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d12 <- d12 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d12 <- d12 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d12 <- d12 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d12 <- d12 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d12 <- d12 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d12 <- d12 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d12 <- d12 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d12 <- d12 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d12 <- d12 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d12 <- d12 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d12 <- d12 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d12 <- d12 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d12 <- d12 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d12 <- d12 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d12 <- d12 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d12 <- d12 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d12 <- d12 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d12 <- d12 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d12 <- d12 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d12 <- d12 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d12 <- d12 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d12 <- d12 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d12 <- d12 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d12 <- d12 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d12 <- d12 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d12 <- d12 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d12 <- d12 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d12_relig <- d12 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d12_relig$religion <- factor(d12_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```


```{r}
d12 <- read_delim(paste0(directory.location, "/CCES Data/CCES12_Common_VV.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```

```{r}
#Subetting releventolums
d12 <- as.data.frame(select(d12, inputstate, cdid, countyfips, gender, race, educ, birthyr, ideo5,  faminc, pid7, votereg, CC417a_4, e2012g, catalist_match, CC332A, CC332B, CC332C, CC332D, CC332E, CC332F, CC332G, CC332H, CC332I, CC332J, CurrentHouseName, CurrentSen1Name, CurrentSen2Name, CC390, CC390b, HouseCand1Party, HouseCand2Party, CC354b, CC354c, inputzip, CC417c, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, V103))

#Renaming columns
colnames(d12) <- c("state", "cd", "fips", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "donate_any", "voter_ver", "vv_matched", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r", "rcv10_r", "house_name", "sen1_name", "sen2_name", "house_vote1", "house_vote2", "p1", "p2", "pres_vote1", "pres_vote2", "zip", "donate_amount", "born_again", "importance", "church_attendance", "prayer", "weight") 
 
#Recode variables
d12$female <- car::recode(d12$female, "1=0;2=1")
d12$ideo <- ifelse(d12$ideo %in% c(6, 98, 99), 99, d12$ideo)
d12$income <- car::recode(d12$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16,31,18,32)=4; c(97,98,99)=99")
d12$reg_voter <- car::recode(d12$reg_voter, "2=0;1=1;3=99")
d12$pid7 <- ifelse(d12$pid7==8, NA, d12$pid7)
d12$voter_ver <- car::recode(d12$voter_ver, "2=0;1=1;c(3,4,5,7,8)=99")
d12$donate_any <- car::recode(d12$donate_any, "1=1;2=0; c(3,98,99)=99")
d12$year <- 2012
d12$voter_ver <- as.numeric(ifelse( d12$voter_ver=="",0, 1)) 
d12$vv_matched <- as.numeric(car::recode(d12$vv_matched, "1=0;2=1"))

d12$house_vote <- ifelse(!is.na(d12$house_vote1), d12$house_vote1, d12$house_vote2)
d12$house_vote <- case_when(d12$house_vote == 1 ~ d12$p1,
                            d12$house_vote == 2 ~ d12$p2,
                            d12$house_vote %in% c(3,4,5,6,7,8,9) ~ "Other")
d12$pres_vote <- ifelse(!is.na(d12$pres_vote1), d12$pres_vote1, d12$pres_vote2)
d12$pres_vote <-car::recode(as.numeric(d12$pres_vote),"2='Dem'; 1='Rep';c(3,4,5)='Other'") 
d12$p1<-NULL
d12$p2<-NULL
d12$house_vote1 <-NULL
d12$house_vote2 <- NULL

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r",  "rcv8_r", "rcv9_r" , "rcv10_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d12[ ,rollcall_votes_r.vec[i]] <- car::recode(d12[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d12 <- bind_cols(d12, d12_relig)
```

```{r}
d13 <- read_delim(paste0(directory.location, "/CCES Data/Common Content Data.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```

```{r}
d13 <- d13 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d13 <- d13 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d13 <- d13 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d13 <- d13 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d13 <- d13 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d13 <- d13 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d13 <- d13 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d13 <- d13 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d13 <- d13 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d13 <- d13 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d13 <- d13 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d13 <- d13 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d13 <- d13 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d13 <- d13 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d13 <- d13 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d13 <- d13 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d13 <- d13 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d13 <- d13 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d13 <- d13 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d13 <- d13 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d13 <- d13 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d13 <- d13 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d13 <- d13 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d13 <- d13 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d13 <- d13 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d13 <- d13 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d13 <- d13 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d13 <- d13 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d13 <- d13 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d13 <- d13 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d13 <- d13 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d13 <- d13 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d13 <- d13 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d13 <- d13 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d13 <- d13 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d13 <- d13 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d13 <- d13 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d13 <- d13 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d13 <- d13 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d13 <- d13 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d13 <- d13 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d13 <- d13 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d13 <- d13 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d13 <- d13 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d13_relig <- d13 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d13_relig$religion <- factor(d13_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d13 <- read_delim(paste0(directory.location, "/CCES Data/Common Content Data.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```

```{r}
#Subetting releventolums
d13 <- as.data.frame(select(d13, inputstate, cdid113, gender, race, educ, birthyr, ideo5, faminc, pid7, votereg, CC332A, CC332B, CC332C, CC332D, CC332E, CC332F, CC332G, CC332H, CurrentHouseName, CurrentSen1Name, CurrentSen2Name, CC13_315, lookupzip, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, weight))

#Renaming columns
colnames(d13) <- c("state","cd", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "house_name", "sen1_name", "sen2_name", "pres_vote", "zip", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d13$female <- car::recode(d13$female, "1=0;2=1")
d13$ideo <- ifelse(d13$ideo %in% c(6, 98, 99), 99, d13$ideo)
d13$income <- car::recode(d13$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,31,32,14,15,16,32)=4; c(97,98,99)=99")
d13$reg_voter <- car::recode(d13$reg_voter, "2=0;1=1;3=99")
d13$pid7 <- ifelse(d13$pid7==8, NA, d13$pid7)
d13$year <- 2013
d13$house_vote <-NA
d13$pres_vote <-car::recode(as.numeric(d13$pres_vote),"1='Dem'; 2='Rep';c(3,4,5)='Other'") 
d13$donate_amount <-NA

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r",  "rcv8_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d13[ ,rollcall_votes_r.vec[i]] <- car::recode(d13[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d13 <- bind_cols(d13, d13_relig)
```


```{r}
d14 <- read_delim(paste0(directory.location, "/CCES Data/CCES14_Common_Content_Validated.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```

```{r}
d14 <- d14 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d14 <- d14 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d14 <- d14 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d14 <- d14 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d14 <- d14 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d14 <- d14 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d14 <- d14 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d14 <- d14 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d14 <- d14 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d14 <- d14 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d14 <- d14 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d14 <- d14 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d14 <- d14 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d14 <- d14 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d14 <- d14 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d14 <- d14 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d14 <- d14 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d14 <- d14 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d14 <- d14 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d14 <- d14 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d14 <- d14 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d14 <- d14 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d14 <- d14 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d14 <- d14 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d14 <- d14 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d14 <- d14 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d14 <- d14 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d14 <- d14 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d14 <- d14 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d14 <- d14 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d14 <- d14 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d14 <- d14 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d14 <- d14 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d14 <- d14 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d14 <- d14 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d14 <- d14 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d14 <- d14 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d14 <- d14 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d14 <- d14 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d14 <- d14 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d14 <- d14 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d14 <- d14 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d14 <- d14 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d14 <- d14 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d14_relig <- d14 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d14_relig$religion <- factor(d14_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d14 <- read_delim(paste0(directory.location, "/CCES Data/CCES14_Common_Content_Validated.tab"), 
    "\t", escape_double = FALSE, trim_ws = TRUE)
```

```{r}
#Subetting releventolums
d14 <- as.data.frame(select(d14,inputstate, cdid, countyfips,  gender, race, educ, birthyr, ideo5, faminc, pid7, votereg, CC417a_4, e2014gvm, matched, CC14_325_1, CC14_325_2, CC14_325_3, CC14_325_4, CC14_325_5,  CC14_331_1, CC14_331_2, CC14_331_3, CC14_331_4, CC14_331_5, CurrentHouseName, CurrentSen1Name, CurrentSen2Name, CC360, CC360x, HouseCand1Party, HouseCand2Party, CC14_317, inputzip, CC417c, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, weight))

#Renaming columns
colnames(d14) <- c("state", "cd", "fips", "female", "race", "edu", "dob", "ideo",  "income", "pid7", "reg_voter", "donate_any", "voter_ver", "vv_matched", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r", "rcv10_r", "house_name", "sen1_name", "sen2_name", "house_vote1", "house_vote2", "p1", "p2", "pres_vote", "zip", "donate_amount", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d14$female <- car::recode(d14$female, "1=0;2=1")
d14$ideo <- ifelse(d14$ideo %in% c(6, 98, 99), 99, d14$ideo)
d14$income <- car::recode(d14$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16,31,18,32)=4; c(97,98,99)=99")
d14$reg_voter <- car::recode(d14$reg_voter, "2=0;1=1;3=99")
d14$pid7 <- ifelse(d14$pid7==8, NA, d14$pid7)
d14$voter_ver <- car::recode(d14$voter_ver, "2=0;1=1;c(3,4,5,7,8)=99")
d14$donate_any <- car::recode(d14$donate_any, "1=1;2=0; c(3,98,99)=99")
d14$year <- 2014
d14$vv_matched <- as.numeric(car::recode(d14$vv_matched, "1=0;2=1"))
d14$voter_ver <- as.numeric(ifelse( d14$voter_ver=="",0, 1)) 

d14$house_vote <- ifelse(!is.na(d14$house_vote1), d14$house_vote1, d14$house_vote2)
d14$house_vote <-case_when(d14$house_vote == 1 ~ d14$p1,
                            d14$house_vote == 2 ~ d14$p2,
                            d14$house_vote %in% c(3,4,5,6,7,8,9) ~ "Other")
d14$p1 <- NULL
d14$p2<-NULL
d14$house_vote1 <- NULL
d14$house_vote2 <- NULL
d14$pres_vote <-car::recode(as.numeric(d14$pres_vote),"1='Dem'; 2='Rep';c(3,4,5)='Other'") 

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r",  "rcv8_r", "rcv9_r" , "rcv10_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d14[ ,rollcall_votes_r.vec[i]] <- car::recode(d14[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d14 <- bind_cols(d14, d14_relig)
```

```{r}
d15 <- read_dta(paste0(directory.location, "/CCES Data/CCES15_Common_OUTPUT_Jan2016.dta"))
```

```{r}
d15 <- d15 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d15 <- d15 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d15 <- d15 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d15 <- d15 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d15 <- d15 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d15 <- d15 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d15 <- d15 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d15 <- d15 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d15 <- d15 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d15 <- d15 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d15 <- d15 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d15 <- d15 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d15 <- d15 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d15 <- d15 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d15 <- d15 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d15 <- d15 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d15 <- d15 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d15 <- d15 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d15 <- d15 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d15 <- d15 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d15 <- d15 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d15 <- d15 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d15 <- d15 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d15 <- d15 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d15 <- d15 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d15 <- d15 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d15 <- d15 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d15 <- d15 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d15 <- d15 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d15 <- d15 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d15 <- d15 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d15 <- d15 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d15 <- d15 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d15 <- d15 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d15 <- d15 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d15 <- d15 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d15 <- d15 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d15 <- d15 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d15 <- d15 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d15 <- d15 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d15 <- d15 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d15 <- d15 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d15 <- d15 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d15 <- d15 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d15_relig <- d15 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d15_relig$religion <- factor(d15_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d15 <- read_dta(paste0(directory.location, "/CCES Data/CCES15_Common_OUTPUT_Jan2016.dta"))
```

```{r}
#Subetting releventolums
d15 <- as.data.frame(select(d15, inputstate, cdid ,gender, race, educ, birthyr, ideo5, faminc, pid7, votereg, CC15_327A, CC15_327B, CC15_327C, CC15_327D, CC15_327E, CC15_327F1, CC15_327F2, CC15_327G, CC15_327H, CurrentHouseName, CurrentSen1Name, CurrentSen2Name, CC15_315, inputzip, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, weight))

#Renaming columns
colnames(d15) <- c("state", "cd", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r", "house_name", "sen1_name", "sen2_name", "pres_vote", "zip", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d15$female <- car::recode(d15$female, "1=0;2=1")
d15$ideo <- ifelse(d15$ideo %in% c(6, 8, 98, 99), 99, d15$ideo)
d15$income <- car::recode(d15$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16,31,18,32)=4; c(97,98,99)=99")
d15$reg_voter <- car::recode(d15$reg_voter, "2=0;1=1;3=99")
d15$pid7 <- ifelse(d15$pid7>=8, 99, d15$pid7)
d15$year <- 2015
d15$cd<-as.numeric(d15$cd)
d15$house_vote <- NA
d15$pres_vote <-car::recode(as.numeric(d15$pres_vote),"1='Dem'; 2='Rep';c(3,4,5,6,8)='Other'") 
d15$donate_amount <- NA

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r",  "rcv8_r", "rcv9_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d15[ ,rollcall_votes_r.vec[i]] <- car::recode(d15[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d15 <- bind_cols(d15, d15_relig)
```

```{r}
d16 <- read_dta(paste0(directory.location, "/CCES Data/CCES16_Common_OUTPUT_Feb2018_VV.dta"))
```

```{r}
d16 <- d16 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d16 <- d16 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d16 <- d16 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d16 <- d16 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d16 <- d16 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d16 <- d16 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d16 <- d16 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d16 <- d16 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d16 <- d16 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d16 <- d16 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d16 <- d16 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d16 <- d16 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d16 <- d16 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d16 <- d16 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d16 <- d16 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d16 <- d16 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d16 <- d16 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d16 <- d16 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d16 <- d16 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d16 <- d16 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d16 <- d16 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d16 <- d16 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d16 <- d16 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d16 <- d16 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d16 <- d16 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d16 <- d16 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d16 <- d16 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d16 <- d16 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d16 <- d16 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d16 <- d16 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d16 <- d16 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d16 <- d16 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d16 <- d16 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d16 <- d16 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d16 <- d16 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d16 <- d16 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d16 <- d16 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d16 <- d16 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d16 <- d16 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d16 <- d16 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d16 <- d16 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d16 <- d16 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d16 <- d16 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d16 <- d16 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d16_relig <- d16 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d16_relig$religion <- factor(d16_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```


```{r}
d16 <- read_dta(paste0(directory.location, "/CCES Data/CCES16_Common_OUTPUT_Feb2018_VV.dta"))
```

```{r}
#Subetting releventolums
d16 <- as.data.frame(select(d16, inputstate, cdid113, countyfips, gender, race, educ, birthyr, ideo5, faminc, pid7, votereg,  CC16_417a_4, CL_E2016GVM, CL_matched, CC16_351A , CC16_351B , CC16_351C , CC16_351D , CC16_351E , CC16_351F , CC16_351G , CC16_351G , CC16_351H , CC16_351I , CC16_351K, CurrentHouseName, CurrentSen1Name, CurrentSen2Name, CC16_367, HouseCand1Party, HouseCand2Party, HouseCand3Party, HouseCand4Party, HouseCand8Party, HouseCand9Party, CC16_364c, lookupzip, CC16_417c, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, commonweight))

#Renaming columns
colnames(d16) <- c("state", "cd", "fips", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "donate_any", "voter_ver", "vv_matched", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r", "rcv10_r", "house_name", "sen1_name", "sen2_name", "house_vote", "p1", "p2", "p3", "p4", "p8", "p9", "pres_vote", "zip", "donate_amount", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d16$female <- car::recode(d16$female, "1=0;2=1")
d16$ideo <- ifelse(d16$ideo %in% c(6, 98, 99), 99, d16$ideo)
d16$income <- car::recode(d16$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16,31,18,32)=4; c(97,98,99)=99")
d16$reg_voter <- car::recode(d16$reg_voter, "2=0;1=1;3=99")
d16$pid7 <- ifelse(d16$pid7==8, NA, d16$pid7)
d16$voter_ver <- as.numeric(ifelse( d16$voter_ver=="",0, 1)) 
d16$donate_any <- car::recode(d16$donate_any, "1=1;2=0; c(3,98,99)=99")
d16$year <- 2016
d16$vv_matched <- as.numeric(car::recode(d16$vv_matched, "1=0;2=1"))

d16$house_vote <-case_when(d16$house_vote == 1 ~ d16$p1,
                            d16$house_vote == 2 ~ d16$p2,
                           d16$house_vote == 3 ~ d16$p3,
                           d16$house_vote == 4 ~ d16$p4,
                           d16$house_vote == 8 ~ d16$p8,
                           d16$house_vote == 9 ~ d16$p9,
                            d16$house_vote %in% c(5,6,7,10,11,12,13,14,15,16) ~ "Other")
d16$p1 <- NULL
d16$p2<-NULL
d16$p3<-NULL
d16$p4<-NULL
d16$p8<-NULL
d16$p9<-NULL

d16$pres_vote <-car::recode(as.numeric(d16$pres_vote),"2='Dem'; 1='Rep';c(3,4,5,6,7)='Other'") 



rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r",  "rcv8_r", "rcv9_r" , "rcv10_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d16[ ,rollcall_votes_r.vec[i]] <- car::recode(d16[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d16 <- bind_cols(d16, d16_relig)
```

```{r}
d17<- read_dta(paste0(directory.location, "/CCES Data/Common Content Data17.dta"))
```

```{r}
d17 <- d17 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d17 <- d17 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d17 <- d17 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d17 <- d17 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d17 <- d17 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d17 <- d17 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d17 <- d17 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d17 <- d17 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d17 <- d17 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d17 <- d17 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d17 <- d17 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d17 <- d17 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d17 <- d17 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d17 <- d17 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d17 <- d17 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d17 <- d17 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d17 <- d17 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d17 <- d17 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d17 <- d17 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d17 <- d17 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d17 <- d17 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d17 <- d17 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d17 <- d17 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d17 <- d17 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d17 <- d17 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d17 <- d17 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d17 <- d17 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d17 <- d17 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d17 <- d17 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d17 <- d17 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d17 <- d17 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d17 <- d17 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d17 <- d17 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d17 <- d17 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d17 <- d17 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d17 <- d17 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d17 <- d17 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d17 <- d17 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d17 <- d17 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d17 <- d17 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d17 <- d17 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d17 <- d17 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d17 <- d17 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d17 <- d17 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d17_relig <- d17 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d17_relig$religion <- factor(d17_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d17<- read_dta(paste0(directory.location, "/CCES Data/Common Content Data17.dta"))
```

```{r}
#Subetting releventolums
d17 <- as.data.frame(select(d17, inputstate, cdid115, gender, race, educ, birthyr, ideo5, faminc_new, pid7, votereg, CC17_340A, CC17_340B, CC17_340C, CC17_340D, CC17_340E, CC17_340F, CC17_340G, CC17_340H, CC17_340I, CurrentHouseName, CurrentSen1Name, CurrentSen2Name, CC17_329, CC17_327, inputzip, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, weights_common))

#Renaming columns
colnames(d17) <- c("state", "cd", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r",  "house_name", "sen1_name", "sen2_name", "house_vote", "pres_vote", "zip", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d17$female <- car::recode(d17$female, "1=0;2=1")
d17$ideo <- ifelse(d17$ideo %in% c(6, 8, 98, 99), 99, d17$ideo)
d17$income <- car::recode(d17$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16)=4; c(97,98)=99")
d17$reg_voter <- car::recode(d17$reg_voter, "2=0;1=1;3=99")
d17$pid7 <- ifelse(d17$pid7==8, NA, d17$pid7)
d17$year <- 2017
d17$cd <- as.numeric(d17$cd)
d17$house_vote <-car::recode(as.numeric(d17$house_vote),"2='Dem'; 3='Rep';c(1,4,5)='Other'") 
d17$pres_vote <-car::recode(as.numeric(d17$pres_vote),"2='Dem'; 1='Rep';c(3,4,5)='Other'")
d17$donate_amount <-NA

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r",  "rcv8_r", "rcv9_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d17[ ,rollcall_votes_r.vec[i]] <- car::recode(d17[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d17 <- bind_cols(d17, d17_relig)
```

```{r}
d18 <- read_dta(paste0(directory.location, "/CCES Data/cces18_common_vv.dta"))
```

```{r}
d18 <- d18 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d18 <- d18 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d18 <- d18 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d18 <- d18 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d18 <- d18 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d18 <- d18 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d18 <- d18 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d18 <- d18 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d18 <- d18 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d18 <- d18 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d18 <- d18 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d18 <- d18 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d18 <- d18 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d18 <- d18 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d18 <- d18 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d18 <- d18 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d18 <- d18 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d18 <- d18 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d18 <- d18 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d18 <- d18 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d18 <- d18 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d18 <- d18 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d18 <- d18 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d18 <- d18 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d18 <- d18 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d18 <- d18 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d18 <- d18 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d18 <- d18 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d18 <- d18 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d18 <- d18 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d18 <- d18 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d18 <- d18 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d18 <- d18 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d18 <- d18 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d18 <- d18 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d18 <- d18 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d18 <- d18 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d18 <- d18 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d18 <- d18 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d18 <- d18 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d18 <- d18 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d18 <- d18 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d18 <- d18 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d18 <- d18 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d18_relig <- d18 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d18_relig$religion <- factor(d18_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d18 <- read_dta(paste0(directory.location, "/CCES Data/cces18_common_vv.dta"))
```

```{r}
#Subetting releventolums
d18 <- as.data.frame(select(d18, inputstate, cdid115, countyfips, gender, race, educ, birthyr, ideo5, faminc_new, pid7, votereg, CC18_417a_6, CL_2018gvm, CL_matched, CC18_328b, CC18_328d, CC18_328e, CC18_328f, CurrentHouseName, CurrentSen1Name, CurrentSen2Name, CC18_353, HouseCand1Party, HouseCand2Party, HouseCand3Party, HouseCand4Party, CC18_317, inputzip, CC18_417c, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, commonweight))

#Renaming columns
colnames(d18) <- c("state", "cd", "fips", "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter", "donate_any", "voter_ver", "vv_matched", "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "house_name", "sen1_name", "sen2_name", "house_vote", "p1", "p2", "p3", "p4", "pres_vote", "zip", "donate_amount", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d18$female <- car::recode(d18$female, "1=0;2=1")
d18$ideo <- ifelse(d18$ideo %in% c(6, 98, 99), 99, d18$ideo)
d18$income <- car::recode(d18$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16)=4; c(97)=99")
d18$reg_voter <- car::recode(d18$reg_voter, "2=0;1=1;3=99")
d18$pid7 <- ifelse(d18$pid7==8, NA, d18$pid7)
d18$voter_ver <- as.numeric(ifelse( as.character(d18$voter_ver)=="",0, 1)) 
d18$donate_any <- car::recode(d18$donate_any, "1=1;2=0; c(3,98,99)=99")
d18$year <- 2018
d18$vv_matched <- as.numeric(car::recode(d18$vv_matched, "1=0;2=1"))

d18$house_vote <-case_when(d18$house_vote == 1 ~ d18$p1,
                            d18$house_vote == 2 ~ d18$p2,
                           d18$house_vote == 3 ~ d18$p3,
                           d18$house_vote == 4 ~ d18$p4,
                            d18$house_vote %in% c(5,6,7,10,11,12,13,14,15,16) ~ "Other")
d18$p1 <- NULL
d18$p2<-NULL
d18$p3<-NULL
d18$p4<-NULL

d18$pres_vote <-car::recode(as.numeric(d18$pres_vote),"1='Dem'; 2='Rep';c(3,4,5)='Other'")

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d18[ ,rollcall_votes_r.vec[i]] <- car::recode(d18[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d18 <- bind_cols(d18, d18_relig)
```

```{r}
d19<- read_dta(paste0(directory.location, "/CCES Data/CCES2019_Common_OUTPUT.dta"))
```

```{r}
d19 <- d19 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d19 <- d19 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d19 <- d19 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d19 <- d19 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d19 <- d19 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d19 <- d19 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d19 <- d19 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d19 <- d19 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d19 <- d19 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d19 <- d19 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d19 <- d19 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d19 <- d19 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d19 <- d19 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d19 <- d19 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d19 <- d19 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d19 <- d19 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d19 <- d19 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d19 <- d19 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d19 <- d19 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d19 <- d19 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d19 <- d19 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d19 <- d19 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d19 <- d19 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d19 <- d19 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d19 <- d19 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d19 <- d19 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d19 <- d19 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d19 <- d19 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d19 <- d19 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d19 <- d19 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d19 <- d19 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d19 <- d19 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d19 <- d19 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d19 <- d19 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d19 <- d19 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d19 <- d19 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d19 <- d19 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d19 <- d19 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d19 <- d19 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d19 <- d19 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d19 <- d19 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d19 <- d19 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d19 <- d19 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d19 <- d19 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d19_relig <- d19 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d19_relig$religion <- factor(d19_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```

```{r}
d19<- read_dta(paste0(directory.location, "/CCES Data/CCES2019_Common_OUTPUT.dta"))
```

```{r}
#Subetting releventolums
d19 <- as.data.frame(select(d19, inputstate, cdid116, countyfips, 
                            gender, race, educ, birthyr, ideo5,  faminc_new, pid7, votereg, 
                            #CC18_417a_6, CL_2018gvm, CL_matched, 
                            CC19_328a, CC19_328b, CC19_328c, CC19_328d, CC19_328e, CurrentHouseName, CurrentSen1Name, CurrentSen2Name,
                            CC19_313, presvote16post, inputzip, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, commonweight))
#Need 

#Renaming columns
colnames(d19) <- c("state", "cd", "fips", 
                   "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter",
                   #"donate_any", "voter_ver", "vv_matched", 
                   "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "house_name", "sen1_name", "sen2_name", "house_vote", "pres_vote", "zip", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d19$female <- car::recode(d19$female, "1=0; 2=1")
d19$ideo <- ifelse(d19$ideo %in% c(6, 98, 99), 99, d19$ideo)
d19$income <- car::recode(d19$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16)=4; c(97)=99")
d19$reg_voter <- car::recode(d19$reg_voter, "2=0;1=1;3=99")
d19$pid7 <- ifelse(d19$pid7==8, NA, d19$pid7)
#d19$voter_ver <- as.numeric(ifelse( as.character(d19$voter_ver)=="",0, 1)) 
#d19$donate_any <- car::recode(d19$donate_any, "1=1;2=0; c(3,98,99)=99")
    d19$voter_ver <- NA
    d19$donate_any<-NA
    d19$vv_matched<-NA
d19$year <- 2019
#d19$vv_matched <- as.numeric(car::recode(d19$vv_matched, "1=0;2=1"))
d19$house_vote <-car::recode(as.numeric(d19$house_vote),"1='Dem'; 2='Rep';c(3,4,5,8,9)='Other'")
d19$pres_vote <-car::recode(as.numeric(d19$pres_vote),"1='Dem'; 2='Rep';c(3,4,5,6,7,98)='Other'")
d19$donate_amount <-NA

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d19[ ,rollcall_votes_r.vec[i]] <- car::recode(d19[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d19 <- bind_cols(d19, d19_relig)
```

```{r}
d20<- read_csv(paste0(directory.location, "/CCES Data/CES20_Common_OUTPUT_vv.csv"))
```

```{r}
d20 <- d20 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d20 <- d20 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d20 <- d20 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d20 <- d20 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d20 <- d20 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d20 <- d20 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d20 <- d20 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d20 <- d20 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d20 <- d20 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d20 <- d20 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d20 <- d20 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d20 <- d20 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d20 <- d20 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d20 <- d20 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d20 <- d20 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d20 <- d20 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d20 <- d20 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d20 <- d20 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d20 <- d20 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d20 <- d20 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d20 <- d20 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d20 <- d20 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d20 <- d20 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d20 <- d20 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d20 <- d20 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d20 <- d20 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d20 <- d20 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d20 <- d20 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d20 <- d20 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d20 <- d20 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d20 <- d20 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d20 <- d20 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d20 <- d20 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d20 <- d20 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d20 <- d20 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d20 <- d20 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d20 <- d20 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d20 <- d20 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d20 <- d20 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d20 <- d20 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d20 <- d20 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d20 <- d20 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d20 <- d20 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d20 <- d20 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d20_relig <- d20 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d20_relig$religion <- factor(d20_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```


```{r}
d20<- read_csv(paste0(directory.location, "/CCES Data/CES20_Common_OUTPUT_vv.csv"))
```

```{r}
#Subetting releventolums
d20 <- as.data.frame(select(d20, inputstate, cdid116, countyfips, 
                            gender, race, educ, birthyr, ideo5, faminc_new, pid7, votereg, 
                            #CC18_417a_6, CL_2018gvm, CL_matched, 
                            CC20_350a, CC20_350b, CC20_350c, CC20_350d, CC20_350e,
                            CC20_350f, CC20_350g, 
                            CC20_351a, CC20_351b,CurrentHouseName, CurrentSen1Name, CurrentSen2Name,
                            CC20_367_voted, CC20_367, HouseCand1Party, HouseCand2Party, CC20_364b, CC20_364a,
                            lookupzip, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, commonweight))
#Need 

#Renaming columns
colnames(d20) <- c("state", "cd", "fips", 
                   "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter",
                   #"donate_any", "voter_ver", "vv_matched", 
                   "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", 
                   "rcv6_r", "rcv7_r",
                   "rcv8_r", "rcv9_r", "house_name", "sen1_name", "sen2_name",
                   "house_votea", "house_voteb", "p1", "p2", "pres_voteb", "pres_votea", "zip", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d20$female <- car::recode(d20$female, "1=0;2=1")
d20$ideo <- ifelse(d20$ideo %in% c(6, 98, 99), 99, d20$ideo)
d20$income <- car::recode(d20$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16)=4; c(97)=99")
d20$reg_voter <- car::recode(d20$reg_voter, "2=0;1=1;3=99")
d20$pid7 <- ifelse(d20$pid7==8, NA, d20$pid7)
#d20$voter_ver <- as.numeric(ifelse( as.character(d20$voter_ver)=="",0, 1)) 
#d20$donate_any <- car::recode(d20$donate_any, "1=1;2=0; c(3,98,99)=99")
    d20$voter_ver <- NA
    d20$donate_any<-NA
    d20$vv_matched<-NA
d20$year <- 2020
#d20$vv_matched <- as.numeric(car::recode(d20$vv_matched, "1=0;2=1"))
d20$house_votea <-case_when(d20$house_votea == 1 ~ d20$p1,
                            d20$house_votea == 2 ~ d20$p2,
                            d20$house_votea %in% c(3,6,7,10,98,99) ~ "Other")
d20$house_voteb <-case_when(d20$house_voteb == 1 ~ d20$p1,
                            d20$house_voteb == 2 ~ d20$p2,
                            d20$house_voteb %in% c(3,6,7,10,98,99) ~ "Other")
d20$house_vote <- ifelse(!is.na(d20$house_votea), d20$house_votea, d20$house_voteb)
d20$pres_vote <- ifelse(!is.na(d20$pres_votea), d20$pres_votea, d20$pres_voteb)
d20$pres_vote <- car::recode(as.numeric(d20$pres_vote),"2='Dem'; 1='Rep';c(3,4,5)='Other'")
d20$donate_amount <-NA


rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d20[ ,rollcall_votes_r.vec[i]] <- car::recode(d20[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d20 <- bind_cols(d20, d20_relig)
```

```{r}
d21<- read_dta(paste0(directory.location, "/CCES Data/CCES21_Common_OUTPUT.dta"))
```

```{r}
d21 <- d21 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d21 <- d21 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d21 <- d21 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d21 <- d21 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d21 <- d21 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d21 <- d21 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d21 <- d21 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d21 <- d21 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d21 <- d21 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d21 <- d21 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d21 <- d21 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d21 <- d21 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d21 <- d21 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d21 <- d21 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d21 <- d21 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d21 <- d21 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d21 <- d21 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d21 <- d21 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d21 <- d21 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d21 <- d21 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d21 <- d21 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d21 <- d21 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d21 <- d21 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d21 <- d21 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d21 <- d21 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d21 <- d21 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d21 <- d21 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d21 <- d21 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d21 <- d21 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d21 <- d21 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d21 <- d21 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d21 <- d21 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d21 <- d21 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d21 <- d21 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d21 <- d21 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d21 <- d21 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d21 <- d21 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d21 <- d21 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d21 <- d21 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d21 <- d21 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d21 <- d21 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d21 <- d21 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d21 <- d21 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d21 <- d21 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d21_relig <- d21 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d21_relig$religion <- factor(d21_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```


```{r}
d21<- read_dta(paste0(directory.location, "/CCES Data/CCES21_Common_OUTPUT.dta"))
```

```{r}
#Subetting releventolums
d21 <- as.data.frame(select(d21, inputstate, cdid117, countyfips, 
                            gender4, race, educ, birthyr, ideo5,  faminc_new, pid7, votereg, 
                            #CC18_417a_6, CL_2018gvm, CL_matched, 
                            CC21_350a, CC21_350b, CC21_350c, CC21_350d, CC21_350e,
                            CC21_350f, CC21_350g,CC21_350h, CC21_350i,
                            CurrentHouseName, CurrentSen1Name, CurrentSen2Name, CC21_362, presvote20post,
                            lookupzip, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, commonweight))
#Need 

#Renaming columns
colnames(d21) <- c("state", "cd", "fips", 
                   "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter",
                   #"donate_any", "voter_ver", "vv_matched", 
                   "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", 
                   "rcv6_r", "rcv7_r",
                   "rcv8_r", "rcv9_r", "house_name", "sen1_name", "sen2_name", "house_vote", "pres_vote", "zip", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d21$female <- car::recode(d21$female, "1=0;2=1")
d21$ideo <- ifelse(d21$ideo %in% c(6, 98, 99), 99, d21$ideo)
d21$income <- car::recode(d21$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16)=4; c(97)=99")
d21$reg_voter <- car::recode(d21$reg_voter, "2=0;1=1;3=99")
d21$pid7 <- ifelse(d21$pid7>=8,99, d21$pid7)
#d21$voter_ver <- as.numeric(ifelse( as.character(d21$voter_ver)=="",0, 1)) 
#d21$donate_any <- car::recode(d21$donate_any, "1=1;2=0; c(3,98,99)=99")
    d21$voter_ver <- NA
    d21$donate_any<-NA
    d21$vv_matched<-NA
d21$year <- 2021
#d21$vv_matched <- as.numeric(car::recode(d21$vv_matched, "1=0;2=1"))
d21$house_vote <-car::recode(as.numeric(d21$house_vote),"1='Dem'; 2='Rep';c(3,4,5,8,9)='Other'")
d21$pres_vote <-car::recode(as.numeric(d21$pres_vote),"1='Dem'; 2='Rep';c(3,4,5,6)='Other'")
d21$donate_amount <- NA

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d21[ ,rollcall_votes_r.vec[i]] <- car::recode(d21[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d21 <- bind_cols(d21, d21_relig)
```

```{r}
d22<- read_csv(paste0(directory.location, "/CCES Data/CES22_Common.csv"))
```

```{r}
d22 <- d22 %>% 
  mutate(white = recode(race, "1=1; else=0")) %>% 
  mutate(black = recode(race, "2=1; else=0"))

## Baptist

d22 <- d22 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = sbc - black) %>% 
  mutate(sbc = recode(sbc, "1=1; else=0"))

d22 <- d22 %>%
  mutate(ibc = recode(religpew_baptist, "5=1; else=0")) 

d22 <- d22 %>%
  mutate(bgc = recode(religpew_baptist, "6=1; else=0")) 

d22 <- d22 %>%
  mutate(mbc = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(mbc = mbc - black) %>% 
  mutate(mbc = recode(mbc, "1=1; else=0"))

d22 <- d22 %>%
  mutate(cb = recode(religpew_baptist, "8=1; else=0")) 

d22 <- d22 %>%
  mutate(fwb = recode(religpew_baptist, "9=1; else=0")) 

d22 <- d22 %>%
  mutate(gabb = recode(religpew_baptist, "10=1; else=0")) 

d22 <- d22 %>%
  mutate(obc = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obc = obc - black) %>% 
  mutate(obc = recode(obc, "1=1; else=0"))

d22 <- d22 %>% 
  mutate(evanbap = sbc + ibc + bgc + mbc + cb + fwb + gabb + obc)

## Methodist
d22 <- d22 %>%
  mutate(fmc = recode(religpew_methodist, "2=1; else=0")) 

d22 <- d22 %>% 
  mutate(evanmeth = fmc)

##Non-Denom

d22 <- d22 %>% 
  mutate(hiatt = recode(pew_churatd, "1:3=1; else=0")) %>% 
  mutate(nd = recode(religpew_nondenom, "1:90=1; else=0")) %>% 
  mutate(evannd = nd + hiatt) %>% 
  mutate(evannd =  recode(evannd, "2=1; else=0"))

## Lutheran 

d22 <- d22 %>% 
  mutate(mz = recode(religpew_lutheran, "2=1; else=0")) %>% 
  mutate(wi = recode(religpew_lutheran, "3=1; else=0")) %>% 
  mutate(evanluth = mz + wi)

## Presbyterian

d22 <- d22 %>% 
  mutate(pca = recode(religpew_presby, "2=1; else=0")) %>% 
  mutate(epc = recode(religpew_presby, "6=1; else=0")) %>% 
  mutate(evanpres = pca + epc)

## Pentecostal 

d22 <- d22 %>% 
  mutate(evanpent = recode(religpew_pentecost, "1:90 =1; else=0")) %>% 
  mutate(evanpent = evanpent - black) %>% 
  mutate(evanpent = recode(evanpent, "1=1; else=0"))

## Episcopal 

## Christian #### 
d22 <- d22 %>% 
  mutate(evanxtn = recode(religpew_christian, "1=1; else = 0"))

## None

## Congregregational

d22 <- d22 %>% 
  mutate(evancong = recode(religpew_congreg, "2=1; else=0"))

## Holiness
d22 <- d22 %>% 
  mutate(evanholy = recode(religpew_holiness, "1:90 =1; else=0")) %>% 
  mutate(evanholy = evanholy - black) %>% 
  mutate(evanholy = recode(evanholy, "1=1; else=0"))

## Advent
## None 

## Totaling Up

d22 <- d22 %>% 
  mutate(evangelical = evanbap + evanmeth + evannd + evanluth + evanpres + evanpent + evanxtn + evancong + evanholy) %>% 
  mutate(evangelical = recode(evangelical, "1:4=1; else=0"))

## Making Mainline

d22 <- d22 %>% 
  mutate(abc = recode(religpew_baptist, "2=1; 4=1; else=0")) %>% 
  mutate(black = case_when(race == 2 ~ 1, TRUE ~ 0)) %>% 
  mutate(abc = case_when(abc == 1 & black != 1 ~ 1, TRUE ~ 0)) 

d22 <- d22 %>% 
  mutate(epis = recode(religpew_episcop, "1:90=1; else=0"))

d22 <- d22 %>% 
  mutate(luth = recode(religpew_lutheran, "1=1; 4=1; else=0"))

d22 <- d22 %>% 
  mutate(meth = recode(religpew_methodist, "1=1; 90=1; else=0"))

d22 <- d22 %>% 
  mutate(pres = recode(religpew_presby, "1=1; 90=1; else=0"))

d22 <- d22 %>% 
  mutate(cong = recode(religpew_congreg, "1=1; 3=1; 90=1; else=0"))

d22 <- d22 %>% 
  mutate(doc = recode(religpew_christian, "2:90=1; else=0"))

d22 <- d22 %>% 
  mutate(reform = recode(religpew_protestant, "11=1; else=0"))

d22 <- d22 %>% 
  mutate(mainline = abc + epis + luth + meth + pres + cong + doc + reform) %>% 
  mutate(mainline = recode(mainline, "1:5=1; else=0"))

## Black Protestant 


d22 <- d22 %>% 
  mutate(meth = recode(religpew_methodist, "3:4=1; else=0"))

d22 <- d22 %>%
  mutate(sbc = recode(religpew_baptist, "1=1; else=0")) %>% 
  mutate(sbc = black + sbc) %>% 
  mutate(sbc = recode(sbc, "2=1; else=0"))

d22 <- d22 %>% 
  mutate(nbap = recode(religpew_baptist, "3=1; else=0"))

d22 <- d22 %>%
  mutate(abc = recode(religpew_baptist, "2=1; else=0")) %>% 
  mutate(abc = black + abc) %>% 
  mutate(abc = recode(abc, "2=1; else=0"))

d22 <- d22 %>%
  mutate(miss = recode(religpew_baptist, "7=1; else=0")) %>% 
  mutate(miss = black + miss) %>% 
  mutate(miss = recode(miss, "2=1; else=0"))

d22 <- d22 %>%
  mutate(obap = recode(religpew_baptist, "90=1; else=0")) %>% 
  mutate(obap = black + obap) %>% 
  mutate(obap = recode(obap, "2=1; else=0"))

d22 <- d22 %>%
  mutate(ometh = recode(religpew_methodist, "90=1; else=0")) %>% 
  mutate(ometh = black + ometh) %>% 
  mutate(ometh = recode(ometh, "2=1; else=0"))

d22 <- d22 %>% 
  mutate(apos = recode(religpew_pentecost, "6=1; 7=1; else=0"))

d22 <- d22 %>%
  mutate(open = recode(religpew_pentecost, "90=1; else=0")) %>% 
  mutate(open = black + open) %>% 
  mutate(open = recode(open, "2=1; else=0"))

d22 <- d22 %>%
  mutate(holy = recode(religpew_holiness, "90=1; else=0")) %>% 
  mutate(holy = black + holy) %>% 
  mutate(holy = recode(holy, "2=1; else=0"))


d22 <- d22 %>% 
  mutate(bprot = meth + sbc + nbap + abc + miss + obap + ometh + apos + open + holy) %>% 
  mutate(bprot = recode(bprot, "1:2=1; else=0"))

## Everything Else

d22 <- d22 %>% 
  mutate(catholic = recode(religpew, "2=1; else=0"))

d22 <- d22 %>% 
  mutate(jewish = recode(religpew, "5=1; else=0"))

d22 <- d22 %>% 
  mutate(other = recode(religpew, "3:4=1; 6:8=1; 12=1; else=0"))

d22 <- d22 %>% 
  mutate(none = recode(religpew, "9:11=1; else=0"))

# Finishing up
d22_relig <- d22 %>%
  select(evangelical, mainline, bprot, catholic, jewish, other, none)  %>%
  mutate(religion = case_when(
    evangelical == 1 ~ "Evangelical",
    mainline    == 1 ~ "Mainline",
    bprot       == 1 ~ "Black Protestant",
    catholic    == 1 ~ "Catholic",
    jewish      == 1 ~ "Jewish",
    other       == 1 ~ "Other",
    none        == 1 ~ "None",
    TRUE        ~ NA_character_  # Default case, if needed
  ))  %>%
  select(religion)

# Convert to factor (optional)
d22_relig$religion <- factor(d22_relig$religion, levels = c("Evangelical", "Mainline", "Black Protestant", "Catholic", "Jewish", "Other", "None"))
```


```{r}
d22<- read_csv(paste0(directory.location, "/CCES Data/CES22_Common.csv"))
```

```{r}
#Subetting releventolums
d22 <- as.data.frame(select(d22, inputstate, cdid117, countyfips, 
                            gender4, race, educ, birthyr, ideo5, faminc_new, pid7, votereg, 
                            #CC18_417a_6, CL_2018gvm, CL_matched, 
                            CC22_350a, CC22_350b, CC22_350c, CC22_350d, CC22_350e,
                            CC22_350f, CC22_350g,CC22_350h,
                            CurrentHouseName, CurrentSen1Name, CurrentSen2Name, CC22_367, CC22_367_voted,
                            HouseCand1Party, HouseCand2Party, presvote20post, lookupzip, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, commonweight))
#Need 

#Renaming columns
colnames(d22) <- c("state", "cd", "fips", 
                   "female", "race", "edu", "dob", "ideo", "income", "pid7", "reg_voter",
                   #"donate_any", "voter_ver", "vv_matched", 
                   "rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", 
                   "rcv6_r", "rcv7_r",
                   "rcv8_r", "house_name", "sen1_name", "sen2_name", "house_votea", "house_voteb",
                   "p1", "p2", "pres_vote", "zip", "born_again", "importance", "church_attendance", "prayer", "weight")
 
#Recode variables
d22$female <- car::recode(d22$female, "1=0;2=1")
d22$ideo <- ifelse(d22$ideo %in% c(6, 98, 99), 99, d22$ideo)
d22$income <- car::recode(d22$income, "c(1,2,3)=1; c(4,5,6,7)=2; c(8,9,10,11)=3; c(12,13,14,15,16)=4; c(97)=99")
d22$reg_voter <- car::recode(d22$reg_voter, "2=0;1=1;3=99")
d22$pid7 <- ifelse(d22$pid7==8, NA, d22$pid7)
#d22$voter_ver <- as.numeric(ifelse( as.character(d22$voter_ver)=="",0, 1)) 
#d22$donate_any <- car::recode(d22$donate_any, "1=1;2=0; c(3,98,99)=99")
    d22$voter_ver <- NA
    d22$donate_any<-NA
    d22$vv_matched<-NA
d22$year <- 2022
#d22$vv_matched <- as.numeric(car::recode(d22$vv_matched, "1=0;2=1"))

d22$house_votea <-case_when(d22$house_votea == 1 ~ d22$p1,
                            d22$house_votea == 2 ~ d22$p2,
                            d22$house_votea %in% c(3,4,5,6,7,8,10,98,99) ~ "Other")
d22$house_voteb <-case_when(d22$house_voteb == 1 ~ d22$p1,
                            d22$house_voteb == 2 ~ d22$p2,
                            d22$house_voteb %in% c(3,4,5,6,7,8,10,98,99) ~ "Other")
d22$house_vote <- ifelse(!is.na(d22$house_votea), d22$house_votea, d22$house_voteb)
d22$pres_vote<-car::recode(as.numeric(d22$pres_vote),"1='Dem'; 2='Rep';c(3,4,5,6)='Other'")

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r")

for(i in 1:length(rollcall_votes_r.vec)){
  d22[ ,rollcall_votes_r.vec[i]] <- car::recode(d22[ ,rollcall_votes_r.vec[i]], "1=1;2=0;3=8;4=9;else=NA")
}

d22 <- bind_cols(d22, d22_relig)
```

```{r warning=FALSE}

d09_relig <- NULL
d10_relig <- NULL
d11_relig <- NULL
d12_relig <- NULL
d13_relig <- NULL
d14_relig <- NULL
d15_relig <- NULL
d16_relig <- NULL
d17_relig <- NULL
d18_relig <- NULL
d19_relig <- NULL
d20_relig <- NULL
d21_relig <- NULL
d22_relig <- NULL

d08$state <- as.character(d08$state)
d07$state <- as.character(d07$state)
d09$state <- as.character(d09$state)
d10$state <- as.character(d10$state)
d11$state <- as.character(d11$state)
d12$state <- as.character(d12$state)
d13$state <- as.character(d13$state)
d14$state <- as.character(d14$state)
d15$state <- as.character(d15$state)
d16$state <- as.character(d16$state)
d17$state <- as.character(d17$state)
d18$state <- as.character(d18$state)
d19$state <- as.character(d19$state)
d20$state <- as.character(d20$state)
d21$state <- as.character(d21$state)
d22$state <- as.character(d22$state)

d06$cd <- as.character(d06$cd)
d11$cd <- as.character(d11$cd)
d12$cd <- as.character(d12$cd)
d14$cd <- as.character(d14$cd)
d07$cd <- as.character(d07$cd)
d13$cd <- as.character(d13$cd)
d15$cd <- as.character(d15$cd)
d17$cd <- as.character(d17$cd)
d20$cd <- as.character(d20$cd)
d21$cd <- as.character(d21$cd)
d22$cd <- as.character(d22$cd)

d06$fips <- as.character(d06$fips)
d07$fips <- as.character(d07$fips)
d09$fips <- as.character(d09$fips)
d11$fips <- as.character(d11$fips)

d06$voter_ver<-as.double(d06$voter_ver)

d06$zip <- as.character(d06$zip)
d08$zip <- as.character(d08$zip)
d07$zip <- as.character(d07$zip)
d09$zip <- as.character(d09$zip)
d10$zip <- as.character(d10$zip)
d11$zip <- as.character(d11$zip)
d12$zip <- as.character(d12$zip)
d13$zip <- as.character(d13$zip)
d14$zip <- as.character(d14$zip)
d15$zip <- as.character(d15$zip)
d16$zip <- as.character(d16$zip)
d17$zip <- as.character(d17$zip)
d18$zip <- as.character(d18$zip)
d19$zip <- as.character(d19$zip)
d20$zip <- as.character(d20$zip)
d21$zip <- as.character(d21$zip)
d22$zip <- as.character(d22$zip)


d08$donate_amount <- as.character(d08$donate_amount)
d09$donate_amount <- as.character(d09$donate_amount)
d10$donate_amount <- as.character(d10$donate_amount)
d11$donate_amount <- as.character(d11$donate_amount)
d12$donate_amount <- as.character(d12$donate_amount)
d13$donate_amount <- as.character(d13$donate_amount)
d14$donate_amount <- as.character(d14$donate_amount)
d15$donate_amount <- as.character(d15$donate_amount)
d16$donate_amount <- as.character(d16$donate_amount)
d17$donate_amount <- as.character(d17$donate_amount)
d18$donate_amount <- as.character(d18$donate_amount)
d19$donate_amount <- as.character(d19$donate_amount)
d20$donate_amount <- as.character(d20$donate_amount)
d21$donate_amount <- as.character(d21$donate_amount)
d22$donate_amount <- NA


merged_d <- bind_rows(d06,d07,d08,d09,d10,d11,d12,d13,d14,d15,d16,d17,d18, d19, d20, d21, d22)

d06 <- NULL
d07 <- NULL
d08 <- NULL
d09 <- NULL
d10 <- NULL
d11 <- NULL
d12 <- NULL
d13 <- NULL
d14 <- NULL
d15 <- NULL
d16 <- NULL
d17 <- NULL
d18 <- NULL
d19 <- NULL
d20 <- NULL
d21 <- NULL
d22 <- NULL


```

```{r}
merged_d <- subset(merged_d, select = -c(house_votea, house_voteb, p1, p2, pres_votea, pres_voteb, born_again))

rollcall_votes_r.vec <- c("rcv1_r", "rcv2_r", "rcv3_r", "rcv4_r", "rcv5_r", "rcv6_r", "rcv7_r", "rcv8_r", "rcv9_r", "rcv10_r")

for(i in 1:length(rollcall_votes_r.vec)){
  merged_d[ ,rollcall_votes_r.vec[i]] <- car::recode(merged_d[ ,rollcall_votes_r.vec[i]], "0=0;1=1;2=0;8=8;else=NA")
}

write.csv(merged_d, "Intermediate Data/CCES_merged_policy_responsiveness_missing_roll_call_relig.csv")
```


- female = What is your gender?
    + 0 = Male
    + 1 = Female
  
- race = What racial or ethnic group best describes you?
    + 1 = White
    + 2 = Black or African American
    + 3 = Hispanic or Latino
    + 4 = Asian or Asian-American
    + 5 = Native American
    + 6 = Mixed Race
    + 7 = Other
    + 8 = Middle Eastern
  
- edu = What is the highest level of education you have completed?
    + 1 = Did not graduate from high school
    + 2 = High school graduate
    + 3 = Some college, but no degree (yet)
    + 4 = 2-year college degree
    + 5 = 4-year college degree
    + 6 = Postgraduate degree (MA, MBA, MD, JD, PhD, etc.)

- dob = In what year were you born? 

- ideo = Thinking about politics these days, how would you describe your own political viewpoint?
    + 1 = Very Liberal
    + 2 =	Liberal
    + 3 =	Moderate
    + 4 =	Conservative
    + 5 = Very Conservative
        * 'Not sure' converted to 'NA'

- relig = What is your religious preference?
    + 1 = Protestant
    + 2 = Catholic
    + 3 = Jewish
    + 4 = Muslim
    + 5 = Athiest, Agnostic, or 'Nothing in Particular' (2007: 'None')
    + 6 = Other (non-Christian) / 'Something Else'
    + 7 = Other (Christian; i.e. Mormon, Greek Orthodox, etc.)
    
- income = Thinking back over the last year, what was your family's annual income? (Condensed to 4 categories)
    + 1 = less than $29,999
    + 2 = $30,000 - $69,999
    + 3 = $70,000 - $149,999
    + 4 = more than $150,000
        * Categories condensed to 4

- pid7 = Party Identification 7-Point Scale
    + 1 = Strong Democrat
    + 2 = Weak Democrat
    + 3 = Lean Democrat
    + 4 =Independent
    + 5 =Lean Republican
    + 6 =  Weak Republican
    + 7 = Strong Republican
        * 'Don't Know' recoded as 'NA'
  
- reg_voter= Registered to Vote - Are you registered to vote? (self-reported)
    + 0 = No
    + 1 = Yes
        * 'Don't Know' recoded as '99'
  
- donate_any = Donate Money to Politics During Past Year- During the past year did you donate money to any candidates for office or to political party committees? 
    + 0 = No
    + 1 = Yes
       * Unavailable all odd numbered years
  
- voted_Ver = verified voter turnout
    + 0 = No
    + 1 = Yes
    + 9 = Not verified or other
        * Unavailable all odd numbered years
  
- vv_matched = succesfully matched to voter file
    + 0 = No
    + 1 = Yes
        * Unavailable all odd numbered years
  
- vv_status = voter status
        * Only included when 'vv_match' is unavailable
        * Unavailable all odd numbered years
      
- year = year survey was conducted

- house_name = name of house represenative for respondent (pre-election survey only)
        * Only included for years where congressional roll call votes are not tracked by the CCES (2009, 2011-2022)
        
- sen1_name = name of senior senator for respondent (pre-election survey only)
        * Only included for years where congressional roll call votes are not tracked by the CCES (2009, 2011-2022)

- sen2_name = name of junior senator for respondent (pre-election survey only)
        * Only included for years where congressional roll call votes are not tracked by the CCES (2009, 2011-2022)

- rcv1_r = Roll Call Vote 1 - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = Partial Birth - How about you? If you were faced with this decision, would you vote for or against banning late-term abortion?
        * 2007 = Ban Late Term Abortions - How about you? If you were faced with this decision, would you vote for or against banning late-term abortion?
        * 2008 = Withdraw Troops - Withdraw Troops from Iraq within 180 days
        * 2009 = Lilly Ledbetter 
        * 2010 = American Recovery and Reinvestment Act - Authorizes $787 billion in federal spending to stimulate economic growth in the U.S.
        * 2011 = Recovery and Reinvestment
        * 2012 = 2011 House Budget Plan. The Budget plan would cut Medicare and Medicaid by 42%. Would reduce debt by 16% by 2020.
        * 2013 = Abortion Bill - Would prohibit abortions after the 22nd week of pregnancy.
        * 2014 = Ryan Budget. Budget plan would cut Medicare and Medicaid by 42%. Would reduce debt by 16% by 2020.
        * 2015 = Repeal Affordable Care Act - Would repeal the Affordable Care	Act.
        * 2016 = Supreme Court Nomination. Approve the nomination of Merrick Garland to the Supreme Court of the United States.
        * 2017 = Repeal Affordable Care Act - Would repeal the Affordable Care Act.
        * 2018 = Roll Call Votes - Appoint Neil Gorsuch to the Supreme Court of the United State
        * 2019 = Amend federal laws to prohibit discrimination on the basis of gender identity and sexual orientation.
        * 2020 = Roll Call Votes - Amend federal laws to prohibit discrimination on the basis of gender identity and sexual orientation.
        * 2021 = Establish a commission to investigate the January 6, 2021, attack on the US Capitol.
        * 2022 = Authorize spending up to \$1.9 trillion for COVID relief from March 2021 through September 2021, including extension of unemployment benefits through September 2021, and emergency funding to state and local governments for the fiscal year.

- rcv2_r = Roll Call Vote 2 - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = Stem Cell Research - What do you think? If you were faced with this decision, would you vote for or against federal funds for this research?
        * 2007 = Stem Cell Research Funding - What do you think? If you were faced with this decision, would you vote for or against federal funds for this research?
        * 2008 = Increase Minimum Wage - Increase Minimum Wage from \$5.15 to \$7.25
        * 2009 = Hate Crimes
        * 2010 = State Children's Health Insurance Program - Program insures children in low income households.  Act would renew the program through 2014 and include 4 million additional children. 
        * 2011 = Children's Health Insur
        * 2012 = Simpson-Bowles Budget Plan. Plan would make 15% cuts across the board in Social Security, Medicare, Medicaid, and Defense, as well as other programs. Eliminate many tax breaks for individuals and corporations. Would reduce debt by 21% by 2020.
        * 2013 = Simpson-Bowles Budget Bill -  Plan would make 15% cuts across the board in Social Security, Medicare, Medicaid,
    and Defense, as well as other programs. Eliminate many tax breaks for individuals and corporations. Would reduce debt by 21% by 2020.
        * 2014 = Simpson-Bowles Budget Plan. Plan would make 15% cuts across the board in Social Security, Medicare, Medicaid, and Defense, as well as other programs. Eliminate many tax breaks for individuals and corporations. Would reduce debt by 21% by 2020.
        * 2015 = Keystone	Pipeline - A bill	to approve the Keystone	XL pipeline	from Montana to Texas.
        * 2016 = Trans-Pacific Partnership Act Free trade agreement among 12 Pacific nations (Australia, Brunei, Canada, Chile, Japan, Malaysia, Mexico, New Zealand, Peru, Singapore, and the US).
        * 2017 = Confirm Neil Gorsuch to Supreme Court
        * 2018 = Roll Call Votes - Require that the President get approval from Congress to ease any existing sanctions on Russia.
        * 2019 = Raise the minimum wage to $15 an hour.
        * 2020 = Roll Call Votes - Raise the minimum wage to $15 an hour.
        * 2021 = Authorize spending up to $1.9 trillion for COVID relief from March 2021 through September 2021, including extension of unemployment benefits through September 2021, and emergency funding to state and local governments for the fiscal year.
        * 2022 = Spend \$150 billion a year for 8 years on construction and repair of roads and bridges, rail, public transit, airports, water systems and broadband internet

   
- rcv3_r = Roll Call Vote 3 - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = Iraq Withdrawal-  What do you think? If you were faced with this decision, would you vote for or against a plan to start withdrawing troops this year?
        * 2007 = Withdraw Troops this Year - What do you think? If you were faced with this decision, would you vote for or against a plan to start withdrawing troops this year?
        * 2008 = Stem Cell Research - Allow federal funding of embryonic stem cell research
        * 2009 = Recovery and Reinvest
        * 2010 = American Clean Energy and Security Act -  Imposes a cap on carbon emissions and allows companies to trade allowances for carbon emissions.  Funds research on renewable energy.
        * 2011 = Clean Energy
        * 2012 = The Middle Class Tax Cut Act. Would extend Bush era tax cuts for incomes below \$200,000. Would increase the budget deficit by an estimated \$250 billion.
        * 2013 = Repeal Affordable Care Act - Would repeal the Affordable Care Act.
        * 2014 = Middle Class Tax Cut. Would extend Bush era tax cuts for incomes below \$200,000. Would increase the budget deficit by an estimated \$250 billion.
        * 2015 = 	Iran Sanctions Act - Imposes new sanctions on	Iran, if Iran	does not agree to	reduce its nuclear program by	June 30.
        * 2016 = USA Freedom Act Ends the US government's phone surveillance database program. Allows individual phone companies to keep such databases, and allows the government to access those records if there is reasonable suspicion an individual is connected to a terrorist organization.
        * 2017 = American Health Care
    Act - Would repeal the tax penalties on individuals for not maintaining health coverage and on employers for not offering coverage. Would end subsidies to help people purchase insurance and would end funding for states that expanded Medicaid.
        * 2018 = Roll Call Votes - Impose sanctions on countries and firms doing business with North Korea. Increase President's authority to impose sanctions if North Korea violates U.N. Security Council resolutions regarding that country.
        * 2019 = Confirm Brett Kavanaugh to become a Justice of the Supreme Court of the United States.
        * 2020 = Roll Call Votes - Confirm Brett Kavanaugh to become a Justice of the Supreme Court of the United States.
        * 2021 = Increase accountability for misconduct by police officers, create a national registry of police officers who have been fired for misconduct, and establish stricter officer training requirements.
        * 2022 =  Spend \$2.2 trillion over the next decade to provide universal prekindergarten, subsidies for child care, expanded financial aid for college, housing support, home and community care for older Americans, and to shift the U.S. economy away from fossil fuels to renewable energy and electric cars.
        
- rcv4_r = Roll Call Vote 4 - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = Immigration - What do you think? If you were faced with this decision, would you vote for or against this proposal?
        * 2007 = Illegal Immigrant Citizenship - What do you think? If you were faced with this decision, would you vote for or against this proposal?
        * 2008 = Eavesdrop Overseas Without Court Order - Allow U. S. spy agencies to eavesdrop on overseas terrorist suspects without first getting a court order
        * 2009 = Children's Health
        * 2010 =  Comprehensive Health Reform Act - Requires all Americans to obtain health insurance.  Allows people to keep current provider.  Sets up health insurance option for those without coverage.  Increase taxes on those making more than $280,000 a year. 
        * 2011 = Health Reform
        * 2012 = The Tax Hike Prevention Act. Would extend Bush-era tax cuts for all indviduals, regardless of income. Would increase the budget deficit by an estimated $405 billion.
        * 2013 = Keystone Pipeline -  A bill to approve the Keystone XL pipeline from Montana to Texas and provide for environmental protection and government oversight.
        * 2014 = Tax Hike Prevention Act. Would extend all Bush era tax cuts regardless of income. Would increase the budget deficit by $405billion.
        * 2015 = 	Trans-Pacific	Partnership	Act	- Free trade agreement among 12	Pacific	nations	(Australia,	Brunei,	Canada,	Chile, Japan,	Malaysia,	Mexico,	New	Zealand, Peru, Singapore, the	US	and	Vietnam).
        * 2016 = Trade Adjustment Assistance Act. Provides education assistance and retraining to workers who have lost their jobs as a result of foreign trade.
        * 2017 = Financial CHOICE Act - Allows banks to not be subject to the heightened regulatory requirements of Dodd-Frank by maintaining enough reserve funds withstand a financial downturn. Grants the president the power to fire the head of the Consumer Financial Protection Bureau and the Federal Housing Finance Agency at any time and without cause. Repeals a rule which prevents commercial banks from making speculative investments for their own profits.
        * 2018 = Roll Call Votes - Appoint Brett Kavanaugh to the Supreme Court of the United States
        * 2019 = Require equal pay for women and men who are doing similar jobs and have similar qualifications.
        * 2020 = Roll Call Votes - Require equal pay for women and men who are doing similar jobs and have similar qualifications.
        * 2021 = Prohibit employers from firing or replacing workers who participate in a strike; remove prohibition on workers at one company supporting a strike by workers at another company (also known as secondary strikes).
        * 2022 = Prohibit government restrictions on the provision of, and access to, abortion services.
        
- rcv5_r = Roll Call Vote 5 - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = Minimum Wage - What do you think? If you were faced with this decision, would you vote for or against increasing the minimum wage?
        * 2007 = Increase Federal Minimum Wage - What do you think? If you were faced with this decision, would you vote for or against increasing the minimum wage?
        * 2008 = Health Insurance Program for Children - Fund a $20 billion program to provide health insurance for children in families earning less that $43,000
        * 2009 = Clean Energy
        * 2010 = Appoint Elena Kagan to the U.S. Supreme Court 
        * 2011 = DADT
        * 2012 = Birth Control Exemption. A Bill to let employers and insurers refuse to cover birth control and other health services that violate their religious beliefs.
        * 2013 = Marketplace Fairness Act -  Would allow states and localities to collect sales taxes on sales made over the Internet, not just on as sales made in stores.
        * 2014 = Debt Ceiling. Allow the US government to borrow funds as needed to meet spending obligations and avoid default on US government bonds.
        * 2015 = Normalization of Relations	with Cuba	- Allows the US	government to	renew	normal diplomatic	relations	with Cuba.
        * 2016 = Education Reform. Repeals the No Child Left Behind Act, which required testing of all students and penalized schools that fell below federal standards. Allows states to identify and improve poor performing schools.
        * 2017 = Kate's Law - Increases criminal penalties for individuals in the country illegally who are convicted of certain crimes, deported, and then re-enter the U.S. illegally.
        * 2018 = NA
        * 2019 = Provide permanent resident status to children of immigrants who were brought to the United States by their parents (also known as Dreamers). Provide these immigrants a pathway to citizenship if they meet the citizenship requirements and commit no crimes.
        * 2020 = Roll Call Votes - Provide permanent resident status to children of immigrants who were brought to the United States by their parents (also known as Dreamers). Provide these immigrants a pathway to citizenship if they meet the citizenship requirements and commit no crimes.
        * 2021 = Require that state and local governments with a history of discrimination in voting must obtain approval of changes in election laws from the Department of Justice.
        * 2022 = Provide \$52 billion in grants for American semiconductor manufacturing and research and a tax credit subsidizing 25% of investments in semiconductor manufacturing.
        
- rcv6_r = Roll Call Vote 6 - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = Capital Gains Tax -  What do you think? If you were faced with this decision, would you vote for or against these tax cuts?
        * 2007 = Extend Capital Gains Tax Cuts - What do you think? If you were faced with this decision, would you vote for or against these tax cuts?
        * 2008 = Amendment to Ban Gay Marriage - Constitutional Amendment banning Gay Marriage
        * 2009 = Health Reform
        * 2010 =  Financial Reform Bill - Protects consumers against abusive lending.  Regulates high risk investments known as derivatives.  Allows government to shut down failing financial institutions. 
        * 2011 = Don't Ask Don't Tell
        * 2012 = U.S.-Korea Free Trade Agreement. Would remove tariffs on imports and exports between South Korea and the U.S.
        * 2013 = Violence Against Women Act -  Would make grants to assist victims of domestic violence and help law enforcement
    prosecute those cases. Make stalking a crime; strengthen criminal rape statutes.
        * 2014 = Agriculture Bill. Ends price supports for corn, wheat, sugar and other agricultural products. Creates a federally subsidized crop insurance program. Reauthorizes the food stamp program, but cuts 10
        * 2015 = 	Renew	Patriot	Act	Phone	Surveillance Provisions - Renews the National	Security Agency's bulk phone database	created	under	the	Patriot	Act	of 2001.
        * 2016 = Highway and Transportation Funding Act - Authorizes $305 Billion to repair and expand highways, bridges, and transit over the next 5 years.
        * 2017 = Countering America's Adversaries Through Sanctions Act - Places sanctions on Iran, North Korea, and Russia. Sets into law sanctions imposed by the Obama administration for Russia's interference in Ukraine, Syria, and the 2016 presidential election. Requires the president to get congressional approval before easing or lifting sanctions on Russia.
        * 2018 = NA
        * 2019 = NA
        * 2020 = Roll Call Votes - Remove President Trump from office for abuse of power
        * 2021 = Amend federal laws to prohibit discrimination on the basis of gender identity and sexual orientation.
        * 2022 = Prohibit large online platforms from giving preference to their own products on the platform at the expense of competing products from another business.
    
- rcv7_r = Roll Call Vote - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = CAFTA - What do you think? If you were faced with this decision, would you vote for or against the trade agreement?
        * 2007 = Ratify CAFTA - What do you think? If you were faced with this decision, would you vote for or against the trade agreement?
        * 2008 = Federal Assistance for Housing Crisis - Federal assistance for homeowners facing foreclosure and large lending institutions at risk of failing
        * 2009 = Appoint Sotomayor 
        * 2010 = End Don't Ask, Don't Tell - Would allow gays to serve openly in the armed services 
        * 2011 = Stem Cell Research
        * 2012 = Repeal Affordable Care Act. Would repeal the Affordable Care Act.
        * 2013 = National Security Agency Phone Surveillance -  Would block funding of the National Security Agency's program which
    gathers details of every phone call made by or to a U. S. phone unless the records were part of a specific investigation.
        * 2014 = NSA Phone Surveillance. Would block funding of the National Security Agency?s program that gathers details of every phone call made by or to a U. S. phone unless the records were part of a specific investigation.
        * 2015 = USA	Freedom	Act	- Ends the US government's phone surveillance database program. Allows individual	phone	companies	to keep such databases.
        * 2016 = Iran Sanctions Act Imposes new sanctions on Iran, if Iran does not agree to reduce its nuclear program by June 30.
        * 2017 = No Sanctuary for Criminals Act - Withholds federal funds from states and localities that do not follow federal immigration laws.
        * 2018 = NA
        * 2019 = NA
        * 2020 = Roll Call Votes - Remove President Trump from office for obstruction of Congress of power
        * 2021 = Require equal pay for women and men who are doing similar jobs and have similar qualifications.
        * 2022 = Appoint Ketanji Brown Jackson to the U.S. Supreme Court.
      
- rcv8_r = Roll Call Vote - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = NA
        * 2007 = NA
        * 2008 = Extend NAFTA - Extend the North American Free trade Agreement (NAFTA) to include Peru and Columbia
        * 2009 = Health Care for all
        * 2010 = Foreign Intelligence Surveillance Act - Allow U.S. spy agencies to eavesdrop on overseas terrorist suspects without first getting a court order 
        * 2011 = Troubled Asset Relief Program
        * 2012 = Keystone Pipeline. A bill to approve the Keystone XL pipeline from Montana to Texas and provide for environmental protection and government oversight.
        * 2013 = Student Success Act -  Would end more than 70 federal education programs and decentralize decision-making to state and local government; promotes Charter Schools; eliminates federal intervention in poor performing schools.
        * 2014 = Cloture. Would amend Senate rules by decreasing the number of votes (from 60 to 51) required to end debate on judicial and executive nominations.
        * 2015 = Trade Adjustment	Assistance Act. - Provides education	assistance and	retraining to workers	who	have lost their	jobs as a	result of foreign	trade.
        * 2016 = Medicare Accountability and Cost Reform Act. Shifts Medicare from fee for-service to pay-for-performance. Ties Medicare payments to doctors to quality of care measures. Requires higher premiums for seniors who make more than $134,000. Renews the Children Health Insurance Program (CHIP).
        * 2017 = Appointment of Betsy DeVos as Secretary of Education
        * 2018 = NA
        * 2019 = NA
        * 2020 = Stimulus bills - In March, the CARES Act proposed to spend \$ 2 trillion in emergency and health care assistance for individuals, families, and businesses, including up to \$ 1,200 per individual and \$500 per child.
        * 2021 = Increase federal spending on high-tech research and development (from \$10 billion to \$30 billion a year), for robotics, computing, advanced manufacturing, and Internet security.
        * 2022 = Spend \$369 billion for tax credits to encourage the production of solar panels, wind turbines, and batteries; lowers Affordable Care Act health care premiums; reduces the deficit by \$300 billion by allowing Medicare to negotiate the cost of some prescription drugs and making changes to the tax code.
        
- rcv9_r = Roll Call Vote - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = NA
        * 2007 = NA
        * 2008 = Bank Bailout - U. S. Government's $700 Billion Bank Bailout Plan
        * 2009 = NA
        * 2010 = Embryonic Stem Cell Research Allow federal funding of embryonic stem cell research
        * 2011 = NA
        * 2012 = Affordable Care Act of 2010. Requires all Americans to obtain health insurance. Allows people to keep current provider. Sets up health insurance option for those without coverage. Increases taxes on those making more than $280,000 a year.
        * 2013 = NA
        * 2014 = Birth Control Exemption. A Bill to let employers and insurers refuse to cover birth control and other health services that violate their religious beliefs.
        * 2015 = Violence	Against	Women	Act. - Provides funds	to assist victims of domestic violence and help law	enforcement	prosecute	those	cases.		Makes	stalking a crime; strengthens criminal rape	statutes.
        * 2016 = Repeal Affordable Care Act. Would repeal the Affordable Care Act of 2009 (also known as Obamacare).
        * 2017 = Consolidated Appropriations Act of 2017 - Funds the government through the end of the fiscal year. Increases defense spending and funding for the National Institutes of Health. Includes funding for border security, but funds could not be used for a border wall. Also includes funding for wildfire relief, Puerto Rico's Medicaid program, the National Endowment for the Humanities, Planned Parenthood and healthcare for coal miners.
        * 2018 = NA
        * 2019 = NA
        * 2020 = Stimulus bills -- In May, the HEROES ACT proposed to spend an additional \$3 trillion, including \$1 trillion for state and local governments and hospitals, spend \$200 billion in hazard pay for essential workers, and give households an additional \$1,200 to \$6,000.
        * 2021 = Spend \$150 billion a year for 8 years on construction and repair of roads and bridges, rail, public transit, airports, water systems and broadband internet
        * 2022 = NA
      
- rcv10_r = Roll Call Vote - Respondent's Position
    + 0 = Oppose
    + 1 = Favor
    + 9 = Don't know
        * 2006 = NA
        * 2007 = NA
        * 2008 = NA
        * 2009 = NA
        * 2010 = Troubled Asset Relief Program - $700 billion loans to banks to stabilize finance
        * 2011 = NA
        * 2012 = End Dont Ask, Dont Tell. Would allow gays to serve openly in the armed services.
        * 2013 = NA
        * 2014 = US - Korea Free Trade Agreement. Would remove tariffs on imports and exports between South Korea and the US.
        * 2015 = NA
        * 2016 = Minimum wage. Raises the federal minimum wage to $12 an hour by 2020.
        * 2017 = NA
        * 2018 = NA
        * 2019 = NA
        * 2020 = NA
        * 2021 = NA
        * 2022 = NA

